See List Comprehensions from Socratica.
List comprehensions are consise ways of executing functions on lists (and sets and dicts). They are inspired by mathematical notation and the Haskell programming language.
In math, the common ways to describe lists (or sets, or tuples, or vectors) are:
In other words, you'll find that the above definitions tell you the following:
S is a sequence that contains values between 0 and 9 included, and each value is raised to the power of two.
The sequence V, on the other hand, contains the value 2 that is raised to a certain power x. The power x starts from 0 and goes till 12.
Lastly, the sequence M contains only the even elements from the sequence S.
See https://www.datacamp.com/community/tutorials/python-list-comprehension
Below we give examples of equivalent computations using iteration, map, and list comprehensions.
a = []
for n in str(12345):
a.append(n)
a
['1', '2', '3', '4', '5']
a1 = list(map(lambda x: x, "12345"))
a1
['1', '2', '3', '4', '5']
a2 = [x for x in str(12345)]
a2
['1', '2', '3', '4', '5']
a == a1 == a2
True
list(map(lambda x: x*x, range(10)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
list(map(lambda x:x-3,map(lambda x: x*x,range(10))))
[-3, -2, 1, 6, 13, 22, 33, 46, 61, 78]
b = []
for n in str(12345):
b.append(int(n))
b
[1, 2, 3, 4, 5]
b1 = list(map(int,"12345"))
b1
[1, 2, 3, 4, 5]
b2 = [int(x) for x in str(12345)]
b2
[1, 2, 3, 4, 5]
b == b1 == b2
True
c = []
for n in str(12345):
c.append(int(n) * int(n))
c
[1, 4, 9, 16, 25]
c1 = list(map(lambda x: int(x) * int(x), "12345"))
c1
[1, 4, 9, 16, 25]
c2 = [int(x) * int(x) for x in str(12345)]
c2
[1, 4, 9, 16, 25]
c == c1 == c2
True
d = []
for n in range(5):
d.append((n, n*n))
d
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
d1 = list(map(lambda x: (int(x), int(x) * int(x)), range(5)))
d1
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
d1a = list(map(lambda x: (x, x*x), range(5))) ## do not need the int()
d1a
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
d2 = [(n, n*n) for n in range(5)]
d2
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
d == d1 == d1a == d2
True
(No map version.)
ee = enumerate([5,4,3,2,1])
ee2 = list(ee)
ee
<enumerate at 0x7fa0c8283240>
ee2
[(0, 5), (1, 4), (2, 3), (3, 2), (4, 1)]
e = []
for (x1,x2) in enumerate("abcde"):
e.append((x1,x2))
e
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
e2 = [(x1,x2) for (x1,x2) in enumerate("abcde")]
e2
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
e == e2
True
Uses filter
instead of map
Uses if
inside list comprehension.
f = []
for x in range(10):
if x % 2 == 0:
f.append(x)
f
[0, 2, 4, 6, 8]
f1 = list(filter (lambda x: x % 2 == 0, range(10)))
f1
[0, 2, 4, 6, 8]
f2 = [x for x in range(10) if x % 2 == 0]
f2
[0, 2, 4, 6, 8]
f == f1 == f2
True
g = []
for x in range(10):
if x > 4:
g.append(x)
g
[5, 6, 7, 8, 9]
g1 = list(filter (lambda x: x > 4, range(10)))
g1
[5, 6, 7, 8, 9]
g2 = [x for x in range(10) if x > 4]
g2
[5, 6, 7, 8, 9]
l = ['morse', 'stiles', '', 'franklin', 'edwards','', "", '', 'branford', '', 'trumbull', '', '', 'hopper']
l
['morse', 'stiles', '', 'franklin', 'edwards', '', '', '', 'branford', '', 'trumbull', '', '', 'hopper']
len(l)
14
list(filter(None, l))
['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']
[x for x in l if x]
['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']
h = 0
for x in range(101):
h += x
h
5050
from functools import reduce
h1 = reduce(lambda x, y: x+y, range(101))
h1
5050
h == h1
True
noprimes = [j for i in range(2, 8) for j in range(i*2, 100, i)]
noprimes
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
primes = [x for x in range(2, 100) if x not in noprimes]
primes
[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]
len(noprimes)
149
noprimes contains duplicates. If we convert it to a set, we will eliminate the duplicates.
set([1,2,3,4,1,2,3,4,5,6,7])
{1, 2, 3, 4, 5, 6, 7}
setnoprimes = set(noprimes)
setnoprimes
{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99}
len(setnoprimes)
73
By using {} instead of [] we can create set comprehensions.
s1 = [x if x % 2 else x + 1 for x in range(10)]
s1
[1, 1, 3, 3, 5, 5, 7, 7, 9, 9]
s2 = {x if x % 2 else x + 1 for x in range(10)}
s2
{1, 3, 5, 7, 9}
We can create literal sets using {}
s3 = {1, 2, 3, 1, 2, 3}
s3
{1, 2, 3}
noprimesset = {j for i in range(2, 8) for j in range(i*2, 100, i)}
noprimesset
{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99}
len(noprimesset)
73
d = {i : x*x for (i,x) in enumerate(range(10))}
d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
d[8]
64
End of list comprehension notebook