Python List Comprehension

Introduction

List Comprehension - list abstraction, list inclusion or list generator.

A list comprehension in the syntax of some programming languages is a way to compactly describe operations for processing lists.

A list can be created without using a list comprehension. For example, with a for loop

>>> squares = [] >>> for i in range(10): squares.append(i * i) >>> squares

Having learned the list comprehension syntax, you can rewrite this solution in just one line

>>> squares = [i * i for i in range(10)] >>> squares

The result is the same in both cases

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Basic syntax

Instead of creating an empty list and appending each element to the end, you simply define the list and its contents at the same time, following this format:

new_list = [expression for member in iterable]

[ expr(item) for item in iterable ]

Every list comprehension in Python consists of three elements (expression, member, iterable):

Another very simple example:

sites = ["urn.su", "heihei.ru", "devhops.ru", "topbicycle.ru"] print([len(site) for site in sites])

[6, 9, 10, 13]

Because the expression is quite flexible for variations, a list comprehension works well where you could use map()

You can study the example with map() here the same with a list comprehension:

>>> txns = [1.09, 23.56, 57.84, 4.56, 6.78] >>> TAX_RATE = .08 >>> def get_price_with_tax(txn): ... return txn * (1 + TAX_RATE) >>> final_prices = [get_price_with_tax(i) for i in txns] >>> final_prices

[1.1772000000000002, 25.4448, 62.467200000000005, 4.9248, 7.322400000000001]

The only difference between this solution and using map() is that a list comprehension in Python returns a list while map() returns a map object.

The Benefit of Using a List Comprehension

List comprehensions are often described as a more «Pythonic» way to create lists than loops or map().

Before agreeing or disagreeing with this statement, it makes sense to understand the advantages of list comprehensions in Python compared to the alternatives.

In this article you will see several examples in which a list comprehension is the best choice.

One of the main advantages of using a list comprehension in Python is that it is a single tool that you can use in a wide variety of situations.

In addition to standard list creation, a list comprehension can also be used for mapping and filtering.

You do not need to use a different approach for each scenario.

This is the main reason why list comprehensions are considered a «Pythonic» approach, since Python includes simple, powerful tools that you can use in a wide variety of situations.

As an additional side benefit, whenever you use a list comprehension in Python, you will not have to remember the correct order of arguments, as you do when calling map().

A list comprehension is also more declarative than loops, which means it is easier to read and understand.

Loops require you to focus on how the list is created.

You need to manually create an empty list, iterate over the elements and append each of them to the end of the list.

Using a list comprehension in Python, you can instead focus on what you want to include in the list, and trust that Python will take care of how the list is built.

How to Add Functionality to List Comprehensions

To understand the full value that list comprehensions can provide, it is useful to understand the range of their possible functionality.

You will also want to understand the changes that are made to list comprehensions in Python 3.8.

Using Conditional Logic

Earlier you have already seen the formula for creating list comprehensions:

new_list = [expression for member in iterable]

This formula is fine, but a more complex version with support for logical conditions is usually used.

The most common way to add a condition (conditional logic) to a list comprehension is to add it to the end of the expression

new_list = [expression for member in iterable (if conditional)]

In this example the conditional expression is right before the closing bracket.

Conditional expressions are useful because they allow list comprehensions to filter out unwanted values without calling the filter() function:

Example with a condition:

sites = ["urn.su", "heihei.ru", "devhops.ru", "topbicycle.ru"] it = ["urn.su", "devhops.ru"] print([len(site) for site in sites if site in it])

[6, 10]

Another example

>>> sentence = 'the rocket came back from mars' >>> vowels = [i for i in sentence if i in 'aeiou'] >>> vowels

['e', 'o', 'e', 'a', 'e', 'a', 'o', 'a']

In this example the conditional expression filters out all characters that are not vowels.

Another example

from math import sqrt from pprint import pprint as pp def is_prime(x): if x < 2: return False for i in range(2, int(sqrt(x)) + 1): if x % i == 0: return False return True print([x for x in range(101) if is_prime(x)])

python filtering_compreh.py

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Filter in a Separate Function

With conditions you can test any valid expression. If you need more complex filtering it can be moved into a separate function:

sentence = 'The rocket, who was named Ted, came back \ from Mars because he missed his friends.' def is_consonant(letter): vowels = 'aeiou' return letter.isalpha() and letter.lower() not in vowels consonants = [i for i in sentence if is_consonant(i)] print(consonants)

['T', 'h', 'r', 'c', 'k', 't', 'w', 'h', 'w', 's', 'n', 'm', 'd', \ 'T', 'd', 'c', 'm', 'b', 'c', 'k', 'f', 'r', 'm', 'M', 'r', 's', 'b', \ 'c', 's', 'h', 'm', 's', 's', 'd', 'h', 's', 'f', 'r', 'n', 'd', 's']

In this example a complex filter is created is_consonant() which is passed as a conditional expression into the list comprehension.

Note that the member value i is passed as an argument to the function.

To read about filtering keyword arguments, see the article *args, **kwargs

Banner image

Pre-modification

A conditional expression can be placed at the end for simple filtering, but if you need to change the member value instead of filtering, the conditional expression must be placed at the beginning:

new_list = [expression (if conditional) for member in iterable]

With this formula you can use conditional expressions to process various input values.

For example, if you have a list of prices, you may need to replace negative values with zeros and leave positive ones unchanged:

>>> original_prices = [1.25, -9.45, 10.22, 3.78, -5.92, 1.16] >>> prices = [i if i > 0 else 0 for i in original_prices] >>> prices

[1.25, 0, 10.22, 3.78, 0, 1.16]

In this example the expression i contains the conditional expression if i > 0 else 0.

This tells Python to output the value i if the number is positive, but if the number is negative to output 0.

Perhaps the variant with a separate function will be clearer:

>>> def get_price(price): ... return price if price > 0 else 0 >>> prices = [get_price(i) for i in original_prices] >>> prices

[1.25, 0, 10.22, 3.78, 0, 1.16]

Now your conditional expression is contained inside the function get_price(), and you can use it as part of the expression for the list comprehension.

Multiple Sources/Conditions

If the data source is lists nested in another list, you can apply for several times

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] b = [x for i in a for x in i] print(b)

python list_comp.py

[1, 2, 3, 4, 5, 6, 7, 8, 9]

# Multi-input Comprehensions c = [(x, y) for x in range(5) for y in range(5)] print(c) # Without comprehension points = [] for x in range(5): for y in range(5): points.append((x, y)) print(points)

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)] [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]

# Multiple if-clauses # Later clauses are nested inside earlier clauses values = [x / (x -y) for x in range(10) if x > 5 for y in range(10) if x - y != 0] print(values) # Without comprehension values = [] for x in range(10): if x > 5: for y in range(10): if x - y != 0: values.append(x / (x - y)) print(values)

[1.0, 1.2, 1.5, 2.0, 3.0, 6.0, -6.0, -3.0, -2.0, 1.0, 1.1666666666666667, 1.4, 1.75, 2.3333333333333335, 3.5, 7.0, -7.0, -3.5, 1.0, 1.1428571428571428, 1.3333333333333333, 1.6, 2.0, 2.6666666666666665, 4.0, 8.0, -8.0, 1.0, 1.125, 1.2857142857142858, 1.5, 1.8, 2.25, 3.0, 4.5, 9.0] [1.0, 1.2, 1.5, 2.0, 3.0, 6.0, -6.0, -3.0, -2.0, 1.0, 1.1666666666666667, 1.4, 1.75, 2.3333333333333335, 3.5, 7.0, -7.0, -3.5, 1.0, 1.1428571428571428, 1.3333333333333333, 1.6, 2.0, 2.6666666666666665, 4.0, 8.0, -8.0, 1.0, 1.125, 1.2857142857142858, 1.5, 1.8, 2.25, 3.0, 4.5, 9.0]

c = [(x, y) for x in range(5) for y in range(x)] print(c)

[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2), (4, 3)]

Nested Comprehensions

# Nested Comprehensions vals = [[y * 3 for y in range(x)] for x in range(5)] print(vals) # Without Comprehension outer = [] for x in range(5): inner = [] for y in range(x): inner.append(y * 3) outer.append(inner) print(outer)

[[], [0], [0, 3], [0, 3, 6], [0, 3, 6, 9]] [[], [0], [0, 3], [0, 3, 6], [0, 3, 6, 9]]

Example from the article about range()

Article author: Andrei Olegovich