List Comprehensions in Python

Introduction

A list is one of the fundamental data types in Python. Every time you come across a variable name that's followed by a square bracket [], or a list constructor, it is a list capable of containing multiple items, making it a compound data type. Similarly, it is also a breeze to declare a new list and subsequently, add one or more items to it.

Short Overview of Lists in Python

Let us create a new populated list:

new_list = [1, 2, 3, 4, 5]
print(new_list) # [1, 2, 3, 4, 5]

Or we can simply use the append() method to add anything you want to the list:

new_list.append(6)
print(new_list) # [1, 2, 3, 4, 5, 6]

If you need to append multiple items to the same list, the extend() method will come in handy. You simply need to pass the list of items to append to the extend method, as shown below:

new_list.extend([7, 8, 9])
print(new_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9]

As you can see, creating a list and appending it with other items is just a piece of cake. You can accomplish this task without having to make multiple calls to the .append() method.

Similarly, you can use a for loop to append multiple items to a list. For example, we will have to write the following piece of code for creating a list of squares for the integers 1-20.

list_a = []
for i in range(1, 20):
    list_a.append(i**2)

What are List Comprehensions in Python?

In the simplest of words, list comprehension is the process of creating a new list from an existing list. Or, you can say that it is Python's unique way of appending a for loop to a list. But, it is already pretty simple to declare a list and append anything you like to it. Isn't it? So, why bother comprehending our lists?

List comprehension, in fact, offers many benefits over traditional lists. To begin with, the code spans over only a single line, making it even easier to declare and read. It is also less cumbersome to comprehend lists than using for loops to declare a new one. Finally, it is also a convenient, quicker, and intuitive way of generating a new, populated list.

Going back to the squares of the integers 1-20, we can obtain the same result using the list comprehension method. Here is what our code will look like now:

list_b = [i**2 for i in range(1, 20)]

Notice how the logic for generating the list items is all wrapped in brackets. We'll cover more about the syntax in the next section.

List Comprehensions Syntax

Before we move forward, it is imperative to explain the syntax of list comprehension. Here is the basic syntax of list comprehension that contains a condition:

[expression for item in list if conditional]

It may seem a bit backward with the expression being before the loop, but this is how it's done! The order is this way, presumably, because it would be difficult to put the expression after the conditional without some type of semicolon, which Python doesn't have.

As you might have already guessed, expression is actually the output we get when we execute the rest of the code in a list comprehension. The code itself is just a for loop iterating over a collection of data. In our example, we are using the expression, or the output, to generate the list of squares.

Note that the conditional is optional, so like in our example above we don't need to include it.

It is also worth mentioning that we have a list to be looped over, the item or items to be iterated, and of course a conditional statement in both the list comprehension and traditional for loops. So each method has the same general constructs, but the difference is how you format and organize them.

We are also going to look at another, more complex example to further understand the concept behind list comprehension.

list_a = [1, 3, 6, 9, 12, 15]
list_b = []
for number in list_a:
    if number % 4 == 0:
        list_b.append(number)

print(list_b)

We are actually looping over the list_a in the above example. Subsequently, we will append an item to list_b if its value is divisible by 4, which is checked using the modulus operator (%). In this example we'd see the following printed to the console:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

[12]

This is because 12 is the only number in that array that is divisible by 4.

Once again, we can just use list comprehension to reduce the total lines of code we have to write to attain the same goal.

As mentioned above, the for loop in the above statement is iterating over the list called list_a. Then it executes the conditional statement that checks if the current value is divisible by 4. Finally, it executes the .append() method when it ascertains that the value is actually divisible by 4.

Now, if you want to write the above piece of code with list comprehension, it would look something like this:

list_a = [1, 3, 6, 9, 12, 15]
list_b = [number for number in list_a if number % 4 == 0]

print(list_b)

As you can see, we have reduced the for loop, which spanned over three lines, to only one line. That is actually the real beauty of list comprehension.

When to Use List Comprehensions

You can use list comprehension in many cases in which you need to generate a list from an iterable. However, the best time to use this method is when you need to add or extract items to a list consistently according to a set pattern. Python developers mostly use them to extract data from an often big collection of items.

Let us suppose you have a list of thousands of current and previous students with their names, their father's names, and addresses. The data of each of the students is further stored in a respective dictionary. But, what if you only want to print their names?

students = [
    {
        "name" : "Jacob Martin",
        "father name" : "Ros Martin",
        "Address" : "123 Hill Street",
    }, {
        "name" : "Angela Stevens",
        "father name" : "Robert Stevens",
        "Address" : "3 Upper Street London",
    }, {
        "name" : "Ricky Smart",
        "father name" : "William Smart",
        "Address" : "Unknown",
    }
]

We do have the option to iterate over the list using the traditional for loop:

names_list = []

for student in students:
    names_list.append(student['name'])

print(names_list)

Although in this example it's only two lines of code for the for loop, we don't even need to write this many lines. We can accomplish the same task by writing only one line of code through the list comprehension method:

names_list = [student['name'] for student in students]

print(names_list)
['Jacob Martin', 'Angela Stevens', 'Ricky Smart']

Conclusion

It is really amazing how list comprehensions actually reduces your workload as well. However, it may seem confusing in the beginning. It is particularly baffling for beginners who have never ventured into this territory before, mostly due to the syntax. You may also find it hard to grasp the concept if you have been programming in other languages because list comprehension does not exist in any of them. The only way to get your head around list comprehension is to practice hard.

Last Updated: July 20th, 2022
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms