List Comprehensions in 5-minutes

A visual introduction to those tricky Pythonic one-liners

Feb 9, 2019

Just show me the code! Quick link: Jupyter Notebook on GitHub

Why would you want to do a Python list comprehension?

To save lines of code.

There’s might be more to it than that but in my experience, this is the only reason — if you have a better one, leave a comment and I’ll update the article.

You can get the exact same thing done with a couple extra lines. But browse any Python Stack Overflow question and chances are you’ll find someone asking for a Pythonic version or a Pythonic one-liner.

List comprehensions are a way of achieving Pythonic one-liners with iterables (lists).

One-line definitions:

  • List = a Python object which can be iterated over (an iterable).

  • Iterated over = going through something one-by-one.

A good example is emptying your shopping cart at the cashier, you pull each item out one by one and put it on the counter, your shopping cart could be considered an iterable.

We’ll use this example to look at some code. We’re shopping at a math store so all we’re buying is numbers.

# Create a list of numbers (our shopping cart)
cart = [3, 4, 12, 17, 19, 21, 23, 26, 30]# Pass items to the cashier
cashier = []
for item in cart:
    cashier.append(item)
print(cashier)# The output is the same as the cart
Output: [3, 4, 12, 17, 19, 21, 23, 26, 30]

What happened here?

  1. We created a list of items in the cart

  2. We created an empty cashierlist (the cashierdidn’t have any of our items yet)

  3. We used a for loop to iterate over each itemin our cart

  4. For each itemwe added it to the cashier list with .append()

  5. Then we checked what items the cashier had with print()

How could we do this same thing a list comprehension?

# Pass items to second cashier but with a list comprehension
cashier_2 = [item for item in cart]
print(cashier_2)# The output is the same as cashier (and the same as cart)
Output: [3, 4, 12, 17, 19, 21, 23, 26, 30]

What happened here?

The exact same steps as above, only written in a different way. Let’s compare.A non-list comprehension and list comprehension comparison of the same code.

  1. cashier is created in blue. On the left, it’s created on its own line. On the right, it’s created at the same time everything else is being created.

  2. cart is being iterated over in green. item is created as the for loop runs. This step is the same as grabbing one item at a time out of your shopping cart and passing it to the cashier.

  3. cashier is updated in the red with each item. The only difference is on the left, an .append() statement is required. The list comprehension on the right negates the need for an .append() statement

We accomplished the same steps in 1 line of code instead of 3. This may not seem like much but say you had a 1000 line Python project. You could potentially reduce your project from 1000 lines to 300. This kind of reduction won’t always happen but it’s an example of how to get the same task done with a Pythonic one-liner.

What else can you do with Python list comprehensions?

Now you’ve got a little bit of an intuition, let’s look at a few more examples.

cashier_3 only deals with even numbers. How could we deal with this?

By using a conditional.

  • Conditional = another way of saying, do something if True, stop if False.

# Make a new cart
cart = [5, 7, 9, 10, 12, 15, 19, 20, 22]# Only give cashier_3 even numbers
cashier_3 = []
for item in cart:
    if item % 2 == 0:
        cashier_3.append(item)
print(cashier_3)# Thanks to the conditional the output is only the even numbers
Output: [10, 12, 20, 22]

How about with a list comprehension?

cashier_3 = [item for item in cart if item % 2 == 0]
print(cashier_3)# The output is the same as above
Output: [10, 12, 20, 22]

What’s happening here?Example of a non-list comprehension and a list comprehension with a conditional.

This is similar to the above example except for the conditional in yellow.

  1. cashier_3 is created in blue.

  2. cart is being iterated over in green. item is being created as the for loop runs.

  3. As each item is passed to cashier_3, it’s being checked to see if it matches up with the conditional in yellow.

  4. cashier_3 is updated in the red with each item as long as it fulfils the conditional in yellow.

Over 100 and odd

cashier_4 only accepts numbers over 100 and are odd. How could you do this?

# Reminder of what cart looks like
cart = [5, 7, 9, 10, 12, 15, 19, 20, 22]# Non-list comprehension for cashier_4
cashier_4 = []
for item in cart:
    item += 100 # add 100 to each item to bring them over 100
    if item % 2 == 1: # check to see if they're odd or not
        cashier_4.append(item)
print(cashier_4)# The output is all odd numbers over 100
Output: [105, 107, 109, 115, 119]

Now with a list comprehension.

# List comprehension for cashier_4
cashier_4 = [item+100 for item in cart if item % 2 == 1]
print(cashier_4)# The output is all odd numbers over 100 (same as above)
Output: [105, 107, 109, 115, 119]

What’s next?

This tutorial only scratches the surface of what’s possible with Python list comprehensions. Once you start to get the hang of them, you’ll start to realise how valuable they are for writing succinct and powerful Pythonic code.

If you’re looking for more, I recommend checking out the Python list comprehension tutorial on DataCamp.

All the code in this article is available on GitHub and a video version of this article is available on YouTube.

Reference : https://towardsdatascience.com/python-list-comprehensions-in-5-minutes-40a68cbe4561

Python – List Comprehensions

In Python you can use list comprehensions to shorten the amount of code that you need to use to accomplish tasks when working with lists. The first time I heard of list comprehensions it sounded very advanced and confusing and when I looked at some example code I was, as I expected confused. Fear not list comprehensions are just as simple as a for loop and once you understand them you will begin to use them in your code to solve problems in much more elegant ways and conserving unneeded lines of code.

What is a List Comprehension and What is its Purpose?

A list comprehension is a concept in Python that’s used to generate a list. Its purpose is to allow a programmer to generate a list in a more fluent and efficient way and use less code in the process than if you were to use a for loop to generate a list. If you do not understand what a List is in python check out my post linked here to become more familiar with lists.

List Comprehension Syntax:

You can think of a list comprehension as a single line for loop enclosed in brackets (thats because it pretty much is) with the operation your performing coming first and the for loop statement coming second. Below I have a visual representation of a traditional for loop and where you place it in a list comprehension

As you can see the code in the blue square (the operation your performing) comes first inside the brackets and the code in the red square (for loop statement minus the colon “:”) comes second.

With this visual representation I hope it is easy to see that a list comprehension can be thought of as a reverse single line for loop.

Example Scenario:

Lets say we have a list of numbers and we need to add 5 to each of those numbers and print them out to the user. Below I have an example using a for loop to accomplish that task and then I solve the same problem using a list comprehension.

For Loop:

#!/usr/bin/python3
 
# List of numbers to be displayed to the user
list_of_numbers = [2, 5, 16, 20]
 
# Define new list
list_to_be_displayed = []
 
# For loop to add 5 to each of the numbers in my list
for number in list_of_numbers:
    list_to_be_displayed.append(number + 5)
 
# Prints the new numbers out to the user
for number in list_to_be_displayed:
    print(number)

Now I will be the first to admit there are more elegant ways to solve this without the need to use 2 for loops, for instance I could have added print(number + 5) inside the first for loop to remove the need for a second for loop but at the same time I would be doing the math twice vs a second for loop. Now lets solve the same problem using a list comprehension.

List Comprehension:

#!/usr/bin/python3
 
# List of numbers to be displayed to the user
list_of_numbers = [2, 5, 16, 20]
 
# Adds 5 to each number in the list of numbers
list_of_numbers = [number + 5 for number in list_of_numbers]
 
# prints out each number to the user
[print(number) for number in list_of_numbers]

Running both of the code snippets gives us the following output:

User@Laptop> forloop.py
7
10
21
25
User@Laptop>

The mechanics of what is happening in both examples is almost the exact same the only exception is we are not creating a new list for the new numbers, instead the list comprehension at line 7 allows us to overwrite our existing list variable with the new values. We have reduced our total overall code by 5 lines but we still use 2 list comprehensions to first do the addition to each of the numbers and the second to print them to the user because we have kept our for loops to a single line.

Generate Strings with List Comprehensions:

Another useful way to use list comprehensions is when generating strings using values in a list, below I have an example based on the original one above to create a human readable string of the added numbers to display to the user that reads “My following numbers are 7 and 10 and 21 and 25”

Using For Loop:

#!/usr/bin/python3

# List of numbers to be displayed to the user
list_of_numbers = [2, 5, 16, 20]

#creates string variable
string = ''

# takes our list, adds 5 to each number and appends that number with the word " and " to the string
for number in list_of_numbers:
    string += '{} and '.format(number + 5)
# Removes the last word " and " from our string
string = string[:-5]
# Prints out to the user
print('My following numbers are {}'.format(string))

When using a for loop we have to create a new variable for the string, then we have to use a for loop to generate the custom part of our string (## and ## and etc.) then we need to remove the final and from the custom part of our string before we can finally print it to the user

Using List Comprehension:

#!/usr/bin/python3

# List of numbers to be displayed to the user
list_of_numbers = [2, 5, 16, 20]

# prints out each number to the user
print('My following numbers are {}'.format(' and '.join([str(number + 5) for number in list_of_numbers])))

Using a list comprehension we have shaved 8 lines off of our code and done all of the work in a single line. Now I will be the first to admit to understand the mechanics of what is happening here you need to analyze line 7 alittle harder but once you start getting comfortable reading more complex list comprehensions that one line it is easy to see what is happening.

Here is the breakdown of what is happening in line 7:

First the list comprehension is generating a new list and that list will contain the product of each of the numbers and 5 storing it as a string in the list that is being generated. That code is the list comprehension itself.

[str(number + 5) for number in list_of_numbers]

Second that list of strings is put together in the following format “## and ## and ## and ##”. That is done by using the join() method which has the list comprehension nested inside of it. Note the join() method is smart enough not to add the word “and” after the last element in the list so we don’t need to handle it like we did before.

' and '.join([str(number + 5) for number in list_of_numbers])

At this point we have our custom string generated, formatted, and ready to be attached to the static part of our string.

Third our custom string is joined with the static part of our string by using the format() method with this code. (If you would like to understand more on string formatting check out my post linked here)

'My following numbers are {}'.format(' and '.join([str(number + 5) for number in list_of_numbers]))

And finally it is printed out to the user by encasing all of that within a print statement.

print('My following numbers are {}'.format(' and '.join([str(number + 5) for number in list_of_numbers])))

Running both of the code snippets gives us the following output:

User@Laptop> test.py
My following numbers are 7 and 10 and 21 and 25
User@Laptop>

Nested List Comprehensions:

You can also nest list comprehensions much like you nest for loops so for instance if you have a list of lists and you need to run a list comprehension over each element in the nested lists this can be accomplished by using a list comprehension. Below I show you an example of adding 1 to each element in a list of lists, the first block I will show you using a for loop and the second i will show you using a list comprehension.

#!/usr/bin/python3
 
# generates list and prints it out
mylist = [[1, 2, 3], [4, 5, 6]]
print('Original List: {}'.format(mylist))
 
# adds 1 to each element of each list within the main list
for list in mylist:
    counter = 0
    for element in list:
        addition = element + 1  # adds 1 to the current element
        list.pop(counter)  # removes current element
        list.insert(counter, addition)  # inserts new number
        counter += 1  # increments counter
 
# prints list
print('Modified List: {}'.format(mylist))
#!/usr/bin/python3
 
# generates list and prints it out
mylist = [[1, 2, 3], [4, 5, 6]]
print('Original List: {}'.format(mylist))
 
# adds 1 to each element of each list within the main list and prints list
mylist = [[element + 1 for element in list] for list in mylist]
print('Modified List: {}'.format(mylist))

Running both of these code examples gives us the following result.

User@Laptop> test.py
Original List: [[1, 2, 3], [4, 5, 6]]
Modified List: [[2, 3, 4], [5, 6, 7]]
User@Laptop>

As you can see running each snippet of code they yield the same result but using a list comprehension takes off 8 lines of code that is the nested for loop and does everything in one line. Using the list comprehension we do not have to create any intermediate variables to count the number of loops or hold temporary data we simply re declare the same variable of our list “mylist” and set it equal to the list comprehension and it takes care of all of the heavy lifting for us which allows us to have cleaner and more understandable code.

If understanding what is happening when translating a nested for loop into a list comprehension is hard for you to see below I have another visual representation as to where each part of the for loop goes into a list comprehension.

Looking at the picture we can see that in the list comprehension we have 2 sets of square brackets with the outer most of the for loop (red) in the outer most bracket, and the inner for loop (blue) inside the inner brackets with the operation we are performing (green) prepended to the beginning inside the inner brackets.

I hope that this article has cleared up any mystery with list comprehensions and I was able to provide some useful examples. If you have any questions please drop a comment below and I will be sure to get back to you with an answer as soon as possible and if this article was helpful please share on social media!

Reference : https://kyletk.com/index.php/2018/03/24/python-list-comprehensions/

Last updated