Python List Comprehensions - Learn Python List Comprehensions
Python List Comprehensions - Learn Python List Comprehensions
Python List Comprehensions - Learn Python List Comprehensions
In this tutorial, we will learn to use Python list comprehensions. Python tutorial is a comprehensive
tutorial on Python language.
transform a list
filter a list
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.
multiply_elements.py
#!/usr/bin/python3
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.
$ ./multiply_elements.py
[2, 4, 6, 8, 10, 12]
Each of the elements was multiplied by two.
fahrenheit_celsius.py
#!/usr/bin/python3
The example creates a new list of Fahrenheit temperatures calculated from a list of Celsius
temperatures.
The calculation is done in the third, expression part of the Python list comprehension.
$ ./fahrenheit_celsius.py
[71.6, 82.4, 91.4, 107.6, 125.6]
filter_positive.py
#!/usr/bin/python3
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 into the new list only if they satisfy the condition.
$ ./filter_positive.py
[2, 12]
filter_by_type.py
#!/usr/bin/python3
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 type(e) == int]
Here we create a list b, which will contain only integer values. The type() function is used to
determine the type of the element.
$ ./filter_by_type.py
[2, 12, 3]
['a', 'c', 'd']
predicate.py
#!/usr/bin/python3
def is_vowel(c):
vowels = 'aeiou'
if c in vowels:
return True
else:
return False
def is_vowel(c):
vowels = 'aeiou'
if c in vowels:
return True
else:
return False
$ ./predicate.py
['e', 'e', 'a', 'e', 'e', 'a', 'e', 'i', 'e']
infront.py
#!/usr/bin/python3
In the example, we transform the values into "even" and "odd" values using the list
comprehension.
$ ./infront.py
['even', 'odd', 'even', 'odd', 'even', 'odd', 'even']
multiple_conditions.py
#!/usr/bin/python3
We create a new list of integers from list a, which are greater than 10 and lower than 20.
$ ./multiple_conditions.py
[18, 14, 11, 19]
multiple_for_loops.py
#!/usr/bin/python3
a = [1, 2, 3]
b = ['A', 'B', 'C']
$ ./multiple_for_loops.py
['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']
flatten_list.py
#!/usr/bin/python3
The nums list is a list of nested lists. We flatten the list with a list comprehension.
The first loop goes through the outer list; the second for loop goes through the nested lists.
$ ./flatten_list.py
[1, 2, 3, 3, 4, 5, 6, 7, 8]
nested_list_comprehension.py
#!/usr/bin/python3
M1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The example defines a matrix and a list comprehension creates a transposed matrix from the
original matrix.
$ ./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
#!/usr/bin/python3
Prime numbers are simply these numbers that are not included in the no_primes list.
$ ./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]