Check out a free preview of the full Intermediate Python course
The "List Comprehensions" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:
Nina demonstrates how to create lists recursively.
Transcript from the "List Comprehensions" Lesson
[00:00:00]
>> Nina Zakharenko: We talked about simple loops yesterday. We talked about for loops, how loop over each item in a list. List comprehensions are one of those incredible features of Python that I think have been making their way into other programming languages slowly over time. They let you take kind of a for loop and condense it down into just one statement in a way that's actually pretty readable.
[00:00:28]
>> Nina Zakharenko: So we'll start with list comprehensions.
>> Nina Zakharenko: And,
>> Nina Zakharenko: Let's say I had my list of names.
>> Nina Zakharenko: So just a list of three items. Everyone who's typing along, you can use whatever names you want.
>> Nina Zakharenko: Earlier, I wanted to build a new list that had maybe all the lowercase names in it, right?
[00:01:12]
So I know that if I say, string .lower, it'll give me the lowercase. If I say string.upper, it will give me the uppercase. Okay, let's say I wanted to do uppercase names, let's say I wanted to shout them, right? Reading them and shouting, you know that these names are kind of really important.
[00:01:32]
So before, I would make my list of uppercase names and I would go through in a for loop, right? for name in names. I would say that the upper_case_name would be equal to name.upper(). And then what action would I need to take next to build up my list of upper_names with these values?
[00:02:03]
How do we add to the end of a list?
>> Speaker 2: Append.
>> Nina Zakharenko: Append, so I would say upper_names.append, and my upper_case_name. Okay, if we look in our upper_names now, we'll see that we've looped through this list, we've taken some action on it, right, but,
>> Nina Zakharenko: This is a lot of code, and it's a pretty common programming paradigm.
[00:02:31]
We do this type of programming all the time. And so Python came up with a really handy shortcut for it. To do a list comprehension, you start with the list syntax, so the square brackets. And then as your kind of getting used to these, I find that the order really trips people up.
[00:02:54]
And so I tend to basically write mine backwards. I think about, what do I wanna loop over, and I type that first. So what were we looping over in our for-loop?
>> [INAUDIBLE].
>> Nina Zakharenko: Yeah, so I would say, for name in names,
>> Nina Zakharenko: So that's the second half of our list comprehension.
[00:03:15]
Now, move your cursor back to the beginning and place it right to the right side of that first opening bracket. The first part of the list comprehension is the action that we want to take. So what was I doing with my name in the previous example?
>> Speaker 2: Uppercase.
[00:03:36]
>> Nina Zakharenko: Yeah, I was making it uppercase. So here, on the left-hand side, I would say name.upper().
>> Nina Zakharenko: What result do we get when we run this?
>> Speaker 2: It lists the uppercase and [INAUDIBLE].
>> Nina Zakharenko: That's right, a list of uppercase names. Pretty useful, right?
>> Nina Zakharenko: Let's say we wanted to build a list of,
[00:04:02]
>> Nina Zakharenko: Square's, right? What function can we use in Python to generate a list of number's in a sequence?
>> Speaker 2: Range.
>> Nina Zakharenko: Range, right. So a quick recap of range. If we call range without doing any sort of looping over it, we'll see this kind of range object, it's because each object in it is generated on the fly, which is great, right?
[00:04:28]
It's great for performance. It's great for memory. As we're learning, if we want a peak inside of range, just maybe we forget the syntax.
>> Nina Zakharenko: And just a quick note here for everyone who's not paying attention. Don't call range on a huge list of numbers and call list around it cuz Nina told you so.
[00:04:58]
Cuz that is not what I'm saying, okay? So if we want to peek inside of a range, we can call list on it, right? So range with just one number will go up to that number, but not inclusive. So exclusive. If we call range with two numbers, this first number is the starting number, the range will be inclusive of the starting number, exclusive of the ending number.
[00:05:29]
>> Nina Zakharenko: And the third argument to range is a step. So if I wanted to go through all the even numbers, or I'm sorry, not all the even numbers. If I wanted to go from 1 to 4 and add 2 each time, I would get 1 and 3. So that's how range works.
[00:05:48]
Let's say I wanted to make a list comprehension that went through all of the numbers from 0 to 5 and print it out, the square. So I would say, for num in range, if I wanted the numbers from 0 to 5, what value would I need to put in range?
[00:06:11]
>> Speaker 2: 6.
>> Nina Zakharenko: 6, okay, so this is what we're going to loop over in our list comprehension. And then moving back to the start of the line.
>> Nina Zakharenko: Windows users, I don't know, Mac users, Ctrl+A might help you get your line to the start, or your cursor to the start of the line.
[00:06:32]
And if I wanted to do the scores, I would just say, num for num.
>> Nina Zakharenko: That's quickly all of our squares. We can return a list of any of the other types that we've talked about. In our first example, we saw strings. In our second example, we saw a number.
[00:06:57]
>> Nina Zakharenko: Let's take a look at a more complicated data type. Let's say I wanted to return a list of tuples, and,
>> Nina Zakharenko: The way that I would do that, so I have names variable here, I would make my list comprehension. I'm gonna loop over the name in names.
[00:07:20]
>> Nina Zakharenko: And what I want to return is a tuple. So I'm gonna put those parentheses around it. I want a tuple that has the string length in it, for example, along with the length of the name. How do we get the length of a string?
>> Speaker 2: With length, len.
[00:07:40]
>> Nina Zakharenko: That's right.
>> Nina Zakharenko: Don't forget an extra parenthesis here. And that would get the length of my name. So any variable that's on the right side of this for statement, that's available for us to use in the left side of this equation.
>> Nina Zakharenko: Yesterday we used tuple unpacking to loop through two items at once, for example, if we were looping over a list of tuples.
[00:08:12]
So, okay, call this list comprehension, I get the word length followed by the length of each string. One last thing I wanna show with split and join, I've shown you kind of f strings, right? So let's say the name is Nina, and my f string, starts with an f, it can say, The name is, followed by {name}.
[00:08:46]
>> Nina Zakharenko: If I wanted to do the same thing for a list of names and print them out in a comma separating string, I can do that with a join, right?
>> Nina Zakharenko: So if I just had kind of my one string here, it'd be kind of funky, it's because it's not a list.
[00:09:26]
>> Nina Zakharenko: You can pass a list comprehension into join, right? Join takes a list. So I can say, for name in names, like I did before. And I can just go with the right, put my cursor to the right of that opening bracket, and I can put an f string in here and say, name is name.
[00:09:55]
>> Nina Zakharenko: So we're just putting some of these pieces together.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops