ZetCode

Python list comprehensions

last modified October 29, 2026 Python 3.15

In this article we learn to use Python list comprehensions.

Python list comprehension

A list comprehension is a syntactic construct which creates a list based on an existing iterable. List comprehensions provide a concise way to create lists. It is a common requirement to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

Python list comprehension usage

A list comprehension can be used to:

The syntax of a list comprehension was influenced by mathematical notation of sets. The Python syntax was inspired by the Haskell programming language.

S = {x² : x in {0 ... 16}}

This is a mathematical notation for creating a set of integer values.

L = [expression [if condition] for variable in sequence [if condition]]

The above pseudo code shows the syntax of a list comprehension. It consists of three parts: a for loop, optional conditions, and an expression. A for loop goes through the sequence. For each loop an expression is evaluated if the condition is met. If the value is computed it is appended to the new list. There can be multiple for loops and if conditions.

Python list comprehension transforming list

The following example transforms a list into another list with the help of the list comprehension.

multiply_elements.py
a = [1, 2, 3, 4, 5, 6]

b = [e * 2 for e in a]
print(b)

In the first example, we create a new list from an existing list by multiplying each element by 2.

b = [e * 2 for e in a]

Each of the elements of the a list is multiplied by 2 and the result is added to the new b list.

$ python multiply_elements.py
[2, 4, 6, 8, 10, 12]

Calculating Fahrenheit temperatures from Celsius

We have a list of temperatures in Celsius. We want to create a new list of temperatures expressed in Fahrenheit.

fahrenheit_celsius.py
celsius = [22, 28, 33, 42, 52]

fahr = [e * 9 / 5 + 32 for e in celsius]
print(fahr)

The example creates a new list of Fahrenheit temperatures calculated from a list of Celsius temperatures.

fahr = [e * 9 / 5 + 32 for e in celsius]

The calculation is done in the expression part of the list comprehension.

$ python fahrenheit_celsius.py
[71.6, 82.4, 91.4, 107.6, 125.6]

Python list comprehension filtering list

In the following example, we are going to filter a list with a list comprehension.

filter_positive.py
a = [-4, 2, 0, -1, 12, -3]

b = [e for e in a if e > 0]
print(b)

We have a list of integers. We create a new list where we only include positive integers.

b = [e for e in a if e > 0]

To include only positive numbers, we use an if condition, which is applied on each of the elements; the elements are included in the new list only if they satisfy the condition.

$ python filter_positive.py
[2, 12]

Filtering by type

The next example filters elements by their type.

filter_by_type.py
a = ['a', 2, 'c', 12, 3, 'd']

b = [e for e in a if isinstance(e, int)]
c = [e for e in a if isinstance(e, str)]

print(b)
print(c)

We have a list of elements, which are integers and strings. We create two new lists; one having only integers and one only strings.

b = [e for e in a if isinstance(e, int)]

Here we create a list b, which will contain only integer values. The isinstance function checks whether the element is an instance of the given type.

$ python filter_by_type.py
[2, 12, 3]
['a', 'c', 'd']

Python list comprehension predicate

A predicate is a function that returns a boolean value. If the condition is too complex, we can put it into a predicate.

predicate.py
def is_vowel(c):
    return c.lower() in 'aeiou'


sentence = 'There are eagles in the sky.'

vowels = [c for c in sentence if is_vowel(c)]
print(vowels)

The example picks out all vowels from a sentence.

def is_vowel(c):
    return c.lower() in 'aeiou'

The function is a predicate. It returns True for a vowel character, regardless of its case.

vowels = [c for c in sentence if is_vowel(c)]

The logic of the if condition is delegated to the is_vowel predicate.

$ python predicate.py
['e', 'e', 'a', 'e', 'e', 'a', 'e', 'i', 'e']

The if condition in front

A conditional expression can be placed in the expression part as well. This way the data can be transformed.

even_odd.py
data = ['even' if i % 2 == 0 else 'odd' for i in range(7)]
print(data)

In the example, we transform the values into 'even' and 'odd' values using the list comprehension. Unlike the if clause at the end, a conditional expression in front does not filter any elements; it needs the else part and produces one value for each element.

$ python even_odd.py
['even', 'odd', 'even', 'odd', 'even', 'odd', 'even']

Python list comprehension multiple if conditions

It is possible to use multiple if conditions in Python list comprehensions.

multiple_conditions.py
a = [9, 2, 18, 14, 22, 11, 7, 19, 23]

b = [e for e in a if e > 10 if e < 20]
print(b)

c = [e for e in a if 10 < e < 20]
print(c)

We create a new list of integers from list a, which are greater than 10 and lower than 20.

b = [e for e in a if e > 10 if e < 20]

In this list comprehension, we use two if conditions. An element is included only if it satisfies both of them.

c = [e for e in a if 10 < e < 20]

The same filter can be written with a single chained comparison.

$ python multiple_conditions.py
[18, 14, 11, 19]
[18, 14, 11, 19]

Python list comprehension multiple for loops

It is possible to have multiple for loops in a Python list comprehension.

multiple_for_loops.py
a = [1, 2, 3]
b = ['A', 'B', 'C']

c = [str(i) + j for i in a for j in b]
print(c)

The example creates a Cartesian product of two lists.

c = [str(i) + j for i in a for j in b]

Two for loops are used to create a Cartesian product. The second loop is nested in the first one, so it runs completely for each element of the first list.

$ python multiple_for_loops.py
['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']

Flattening a list

The next example shows how to flatten a Python list.

flatten_list.py
nums = [[1, 2, 3], [3, 4, 5], [6, 7, 8]]

c = [e for num in nums for e in num]
print(c)

The nums list is a list of nested lists. We flatten the list with a list comprehension.

c = [e for num in nums for e in num]

The first loop goes through the outer list; the second for loop goes through the nested lists. The loops are written in the same order as they would be in nested for statements.

$ python flatten_list.py
[1, 2, 3, 3, 4, 5, 6, 7, 8]

Unpacking in list comprehensions

Since Python 3.15, the expression part of a comprehension can use the * unpacking operator. The elements of each iterable are added to the new list one by one. This is a shorter way to flatten nested lists.

unpacking.py
nums = [[1, 2, 3], [3, 4, 5], [6, 7, 8]]

c = [*num for num in nums]
print(c)

d = [*num[1:] for num in nums if sum(num) > 6]
print(d)

The example flattens the same nested list with unpacking.

c = [*num for num in nums]

For each nested list, *num unpacks its elements into the result. It replaces the second for clause of the previous example.

d = [*num[1:] for num in nums if sum(num) > 6]

Conditions and transformations work as usual. Here we skip the first nested list, whose sum is 6, and drop the first element of the remaining lists. The syntax causes a SyntaxError in Python 3.14 and older. Set and dictionary comprehensions, ** unpacking, and generator expressions are covered in Python unpacking in comprehensions.

$ python unpacking.py
[1, 2, 3, 3, 4, 5, 6, 7, 8]
[4, 5, 7, 8]

The walrus operator in list comprehensions

The assignment expression :=, also called the walrus operator, stores a value computed in the condition so that the expression part can use it.

walrus.py
words = ['sky', 'forest', 'cup', 'Universe', 'war', 'summer']

lengths = [(w, n) for w in words if (n := len(w)) > 3]
print(lengths)

We keep the words longer than three characters together with their lengths.

lengths = [(w, n) for w in words if (n := len(w)) > 3]

The length of each word is computed only once, in the condition, and the result is reused in the tuple. See Python walrus operator for more.

$ python walrus.py
[('forest', 6), ('Universe', 8), ('summer', 6)]

Python nested list comprehensions

The initial expression in a Python list comprehension can be another list comprehension.

nested_list_comprehension.py
m1 = [[1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]]

m1_tr = [[row[i] for row in m1] for i in range(3)]
print(m1_tr)

The example defines a matrix and a list comprehension creates a transposed matrix from the original matrix. The inner comprehension builds one row of the new matrix from the i-th element of every original row.

$ python nested_list_comprehension.py
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Sieve of Eratosthenes

Sieve of Eratosthenes is an ancient algorithm to compute prime numbers. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. The algorithm iteratively marks as composite (i.e., not prime) the multiples of each prime, starting with the multiples of 2.

sieve_of_eratosthenes.py
no_primes = {j for i in range(2, 8) for j in range(i * 2, 100, i)}
primes = [e for e in range(2, 100) if e not in no_primes]
print(primes)

The example computes prime numbers up to 100.

no_primes = {j for i in range(2, 8) for j in range(i * 2, 100, i)}

First, we create a set of numbers that are not primes. It is enough to mark the multiples of numbers up to 7, since the square root of 100 is 10. We use a set comprehension, because checking membership in a set is much faster than in a list.

primes = [e for e in range(2, 100) if e not in no_primes]

Prime numbers are simply the numbers that are not included in the no_primes set.

$ python sieve_of_eratosthenes.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]

Source

Python data structures - Python tutorial

In this article we have presented Python list comprehensions.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.