Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Composing Lists With Comprehensions

00:00 In the previous lesson, I showed you how to use the list .extend() method and its shortcut +=. In this lesson, I’ll be showing you how to use a list comprehension.

00:11 Python has a language feature called comprehensions that allow you to create data structures in-line in your code. There are comprehensions for lists, dictionaries, and sets.

00:22 This is a short form where you can often take a multi-line loop that constructs a data structure and change it into a single line of code. Comprehensions are more limited than general loop cases, which means the busy folks on the compiler team are able to optimize them and make them more performant than the general loop equivalent.

00:42 The downside is they’re kind of compact and can be a bit hard to read, doubly so if you start nesting comprehensions in comprehensions.

00:52 This is a list comprehension that iterates over a value named people and creates a list consisting of the .name attributes for each person object in the people iterable.

01:03 When I try to read a list comprehension, I find it easier if I break it down into parts starting with the middle. This bit here is the same as the declaration part of a for loop.

01:14 It tells me I’m looping over something and I’m going to name each value in that something person. On the right-hand side is what I’m iterating over.

01:23 Again, similar to how you write a regular for loop. In this case, I’m iterating over people.

01:29 And then on the left side, I have what is participating in the list. You can think of the value here as being the thing being appended to the resulting list for each value in the iteration.

01:41 You can even do minor access things in this spot like I’ve done here. I’m not putting a person object into the list. I’m putting the .name attribute from the person object into the list.

01:53 I can also do calculations here like 10 + value or groupings like making the value part of a tuple. The underlying portion is what determines what values get into the list.

02:06 Let’s consider another one. Here I’m looping over something and I’m going to call each value num. The thing I am iterating over is the response to the range() function, which in this case returns the values from zero to nine.

02:21 What I’m putting in the list is just the value itself. Oddly, I find this one harder to read as you end up with num for num, which just seems weird.

02:31 But remember, whatever’s on the left is what goes in the list. If you think back to previous lessons where I flattened a list of lists by iterating on it, a comprehension can do that as well.

02:43 Let’s go check it out. Once more I’m inside flatten.py, but this time the function is a one-liner. This comprehension’s a bit messier than the examples I just showed you as it actually is a nested comprehension.

03:00 The first for for row in iterates over the outer list, while the second for for item in iterates over each inner list.

03:09 The item value from the nested comprehension gets used on the left-hand side, so it’s the item that populates the list. This really is just a compact way of writing what was in our first approach, the two nested loops.

03:24 Let’s try it out. That old familiar data structure,

03:32 my import, and let’s make it go

03:40 and there it is. One more way to flatten the same list.

03:45 Next up, I’ll show you one of the functions in the itertools library.

Become a Member to join the conversation.