How to Use Python List Comprehensions (And When Not to Use Them)
Pinterest Whatsapp
Advertisement

You may have heard of Python’s list comprehension. Maybe it’s even something you’ve used without really understanding. Now is the time to learn, as we cover everything you need to know about list comprehension in Python.

Before getting started, it’s worth refreshing yourself on how arrays and lists work in Python How Arrays and Lists Work in Python How Arrays and Lists Work in Python Arrays and lists are some of the most useful data structures in programming -- although few people use them to their full potential. Read More and how to use Python dictionaries Python Dictionary: How You Can Use It To Write Better Code Python Dictionary: How You Can Use It To Write Better Code Think of a Python dictionary as unordered sets of key-value pairs. In this article we introduce you to working with them and using them to improve your Python code. Read More .

What Is Python List Comprehension?

List comprehension sounds complex but it really isn’t. In Python, it’s simply a quick way to filter or refine a list based on some criteria.

It saves you having to write several lines of code (especially if you’re in a loop already), and it keeps the readability of your code neat.

Be careful, however, as list comprehension is not always the answer. It’s easy to get carried away and write complex comprehensions that are tough to read. Sometimes writing more code is better, especially if it helps readability. Stick to simple tasks, and keep code to a single responsibility.

How to Use List Comprehensions in Python

Note: These examples all use Python 3.6. If you’re not sure of the differences between Python 3 and Python 2, then make sure you read our Python FAQ The Most Frequently Asked Questions About Python Programming The Most Frequently Asked Questions About Python Programming In this article, we'll walk you through everything you need to know about Python as a beginner. Read More , where we cover this question and more.

Consider this bit of code that copies an array and turns each letter in that array into an uppercase. It does so by looping through each item in the array:

letters = ['a', 'b', 'c', 'd']
print(letters)

upper_letters = []
for letter in letters:
  result = letter.upper()
  upper_letters.append(result)

print(upper_letters)

Learn Python list comprehension

Now here’s the same exact logic, except done in a single line of code using a basic Python list comprehension:

letters = ['a', 'b', 'c', 'd']
print(letters)

upper_letters = [x.upper() for x in letters]

print(upper_letters)

Learn Python list comprehension

As you can see, the result is exactly the same, but the process involves significantly more code without list comprehension.

Let’s break this simple example down.

This example creates a list called letters. This stores the lowercase letters “a”, “b”, “c”, and “d”. Supposing you want all these list elements to be uppercase? Well, without list comprehension, you have to create a new list to store the result (called upper_letters), loop over every element in the letters list, convert each letter (and store it in result—optional but good practice), and then append the uppercase letter to the new list. What a lot of work!

The list comprehension here is almost exactly equivalent to the loop alternative. It effectively says “for every letter in the letters list, convert them to uppercase, and return the result as a new list.”

List comprehension can only work on lists, and must return a new list. Let’s dig deeper.

There are three parts to a list comprehension (we’ll cover the third part below). List comprehensions must start and end with square brackets ([ and ]). This is how it was designed, and lets Python know that you’ll be working with a list.

Inside the square brackets, you need to start with the result. This is what you want to do with each list element.

In the example above, the following code converts each element (referenced by the variable name x) to upper case by using the upper() method, which is part of the Python core library:

[x.upper() # will not run, only half the comprehension at this point

Next, you need to tell Python which list to work on, and assign each individual element to a variable. This is exactly the same as the for loop in the long winded example:

for x in letters

Every time the loop goes over the list, the value of x will change to whatever the current element is. It will start off as “a”, and then “b”, and so on.

If you put it all together (and assign it to a variable called upper_letters), you’ll be done:

upper_letters = [x.upper() for x in letters]

Now, upper_letters will contain a list of uppercase letters, starting at “A”, and then “B” and so on.

The Third Part of List Comprehension in Python

As we mentioned above, there’s a third part to list comprehension.

Once you’ve done the two steps above, you can include an optional condition. This is like using an if statement to say “make me a new list, based on this old list, but only include elements which meet my criteria”.

Here’s what it looks like:

ages = [1, 34, 5, 7, 3, 57, 356]
print(ages)

old_ages = [x for x in ages if x > 10]
print(old_ages)

Learn Python list comprehension

This example uses a new list called ages. The old_ages list is assembled using a list comprehension. The if condition on the end means that only list elements which meet the criteria are inserted into the new list. In this example, any ages greater than ten are allowed.

When Not to Use Python List Comprehensions

List comprehension is amazing once you’ve got the hang of it, but it’s not useful in every circumstance. You probably shouldn’t use it when you need more than one condition:

old_ages = [x for x in ages if x > 10 and x < 100 and x is not None]

This code works, but it’s starting to get long and confusing. Similarly, anything more than a simple function call may not work. In this example, you’ll get an error:

letters = ['a', 'b', 'c', 'd', 2]
print(letters)

upper_letters = [x.upper() for x in letters]
print(upper_letters)

Learn Python list comprehension

This is perfectly valid code, but as you cannot uppercase a number, it won’t work. This is one case when the longer loop is actually preferable, as you’ll be able to do some exception handling:

letters = ['a', 'b', 'c', 'd', 1]
print(letters)

upper_letters = []

for letter in letters:
  try:
    result = letter.upper()
    upper_letters.append(result)
  except AttributeError:
    pass # do nothing

print(upper_letters)

Learn Python list comprehension

Start Putting Python List Comprehensions to Use

Now that you know just how easy list comprehension is in Python, there’s no reason not to be using it. Just remember to keep it simple, and consider the readability above all else.

Maybe you’ll control an Arduino with Python How to Program and Control an Arduino With Python How to Program and Control an Arduino With Python Sadly, it is impossible to directly program an Arduino in Python, but you can control it over USB using a Python program. Here's how. Read More , or what about a DIY Python network security camera DIY Pan and Tilt Network Security Cam with Raspberry Pi DIY Pan and Tilt Network Security Cam with Raspberry Pi Learn how to make remotely viewable pan and tilt security camera with a Raspberry Pi. This project can be completed in a morning with only the simplest of parts. Read More ?

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Jeff
    June 27, 2018 at 6:12 am

    I'm pretty sure list comprehensions can operate on more than just a list, and in python 3, the result isn't a list but rather something that's iterable. You can convert it to a list if you wish, bar = list([x for x in foo]), but most of the time you don't actually need to. It's a nuanced detail, but list comprehensions can go south quickly.

  2. Samuel Nde
    June 27, 2018 at 6:08 am

    Great job man. I love the way you present the material but I think you could also do something about nested `for` in this post.
    I also think it would be great to explain the statement "List comprehension can only work on lists, and must return a new list. Let’s dig deeper." I am very sure that
    `print([word.upper() for word in ('this', 'is', 'not', 'me')])` would yield the output `['THIS', 'IS', 'NOT', 'ME']`

  3. Vishal Gupta
    June 27, 2018 at 2:13 am

    Too much of trivial stuff. Don't write articles just for thr sake of writing and registering internet presence. Write something significant :)