Comprehending Python List Comprehensions—A Beginner’s Guide

Joe Marx
Analytics Vidhya
Published in
6 min readNov 30, 2020

--

Photo by Markus Winkler on Unsplash

“Wow, how did I end up here? I just wanted to find a simple explanation of what the heck a list comprehension is and now I’ve read 10 articles that are all incredibly complicated and I still don’t get why I shouldn’t just use a for loop.” Well, here you are, I’m gonna simply break down how to understand and formulate list comprehensions, and teach you how to garner a promotion from a fictional government agency.

Why?

You may be sitting there saying to yourself, “I understand for loops and how to make them, why waste my finite amount of time on this globe (or disk if you’re a flat earth truther) learning how to do the same thing differently?” Well to that I ask you: “Why did Steve Jobs design the iPhone with an on-screen keyboard instead of a more functional hardware one?” Style, and room for growth. Learning list comprehensions will make your code more streamlined, easier to read, and more adaptable. And Python is a unique language in its readability, so we want to capitalize on every tool we have to make our code easier to read.

Ok, maybe the Steve Jobs metaphor was a tad grandiose, but learning this skill to refine your coding skill will pay dividends down the road.

Photo by ActionVance on Unsplash

A quick aside, you may be wondering if there is a speed or efficiency difference between list comprehensions and for loops. The short answer is yes, and in an unscientific test, list comprehensions turned out to run faster than for loops, below is a snippet showing this. However, there are a few things to note. A list comprehension works by putting all elements of the list in memory before writing it to a variable, which is fine for most cases unless you are dealing with very, very large lists. But to stay true to this blog, I won’t go deeper than this. For more info check out the end of this awesome article from RealPython:

5.5 ms per loop for list comprehension, 8.82ms per loop with for loop

Now to the good stuff

Let’s break down the structure of a list comprehension

I’m going to do this two ways: first I’m going to talk in terms of a standard for loop, then I’m just going to talk in terms of the list comprehension structure. Note that anything formatted like this is code or pseudo-code

If we’re building a list using a for loop, the first step is creating a blank list

lst = []

Next we initialize the for loop by saying for some dummy variable in some iterable

for member in monty_py:

For a simple for loop without conditionals, you then write out your blank list, add .append(), then enter what you want in each item of your new list (usually utilizing your dummy variable).

    lst.append(member.title())

For a list comprehension we skip the first step since we’re making the list directly in the comprehension.

Inside the brackets, the first chunk of code we see is the code that would have gone inside the .append() method — in other words, what we want to go in each item of our new list. NOTE: in most cases we use a dummy variable that is essentially a placeholder for each item in the iterable we’re looping through. In list comprehensions we refer to that dummy variable before we declare it in the for section. That’s a point of confusion for a lot of new Python programmers. The dummy variable is referred to and then specified in the next slice of the list comprehension. This will be clearer with examples below.

We then have our for statement where we declare that dummy variable and indicate what it loops through.

Let’s look at some examples

[x+5 for x in range(6)]OUT[1]: [5, 6, 7, 8, 9, 10]

Let’s break this down from back to front: our iterable is a range object that goes from 0 to 5, we set dummy variable x to loop through each instance in the range, and each item in our new list is x+5. So our first item we output takes the first item in the range 0, sets that to x, then runs the code x+5, then x is set to our next item in the range object, and we repeat running the x+5 code. We loop this until we reach the end of the iterable object range(6) then each instance that was calculated is written into the new list! And bang, zoom there’s your list comprehension.

Now let’s quickly look at an example slightly less contrived and far more silly. Let’s say you’ve been asked by your manager at the Ministry of Silly Walks to check that they’ve been developing unique walks. The tax-payers haven’t been funding your department to create a bunch of cliché or repeated goofy-gaits after all! You realize you can do this by compiling a list of the silly adjectives that describe each silly walk the ministry has helped develop. You are provided a list of strings containing the names of the silly walks and you want to make a list containing just the first word from each item in the list. We want to make our code concise so we decide to do a list comprehension!

I highly recommend opening up a Python environment and attempt doing this yourself before reading on, or at least coding along with me. It really helps with retention!

Ok so the first question we ask is “how do we select the first word from a multi-word string?” One solution is to use the .split() method then select the first item from its result. We can test this with a two-word string:

Looks good, now to apply this to the provided list of silly walks. So we want to apply that .split() method on each item in the list, so we simply need to replace str in our code exploration above with a dummy variable, then create our for loop containing the provided list:

Simple! Your boss is thrilled and is throwing a silly parade in your honor. Compare our list comprehension to using a standard for loop:

Way more going on in that standardfor loop.

Note that in the loops above, walk is a dummy variable. Technically I could have made it for x in silly_walks:or for temp in silly_walks:orfor twenty in silly_walks:, though making it a singular noun referencing what each item in the list actually is helps with your code readability.

Once you get a grasp of the syntax for a list comprehension, it actually will start to feel more intuitive than doing a for loop. You just open with a bracket, write what you want each item in your target list to contain, then write a for statement to cycle through your iterable.

A deeper look at list comprehensions with conditional statements, multiple variables, and other Python object comprehensions can be found in my next article here! Thanks for reading!

--

--

Joe Marx
Analytics Vidhya

Actor turned Data Scientist looking for applause some other way