= [1, 2, 3, 4, 5]
nums = [n * n for n in nums]
squares
squares#> [1, 4, 9, 16, 25]
8 Comprehension & Others
8.1 List Comprehension
8.1.1 Syntax
for item in iterable if condition] [expression
Longer version:
for item1 in iterable1 if condition1
[expression for item2 in iterable2 if condition2
...for itemN in iterableN if conditionN ]
= []
result for item1 in iterable1:
if condition1:
for item2 in iterable2:
if condition2:
...for itemN in iterableN:
if conditionN:
result.append(expression)
8.1.2 Basic
Comprehension with filter
= [1, 2, 3, 4, 5]
numbers ** 2 for n in numbers if n % 2 == 0]
[n #> [4, 16]
= ["a", "b", "c"]
ls
# Equivalent
for x in ls]
[x.upper() #> ['A', 'B', 'C']
str.upper(x) for x in ls]
[#> ['A', 'B', 'C']
8.1.3 Using _
varible
= [1, 2]
d = [list(d) for _ in d]
l
l#> [[1, 2], [1, 2]]
8.1.4 Ex 1: List of Dict
= [
portfolio 'name': 'IBM', 'shares': 100, 'price': 91.1 },
{'name': 'MSFT', 'shares': 50, 'price': 45.67 },
{'name': 'HPE', 'shares': 75, 'price': 34.51 },
{'name': 'CAT', 'shares': 60, 'price': 67.89 },
{'name': 'IBM', 'shares': 200, 'price': 95.25 }
{ ]
Collect all names
= [s['name'] for s in portfolio]
names
names#> ['IBM', 'MSFT', 'HPE', 'CAT', 'IBM']
Find all entries with more than 100 shares [‘IBM’]
= [s['name'] for s in portfolio if s['shares'] > 100 ]
more100
more100#> ['IBM']
Find the total shares*price
= sum([s['shares']*s['price'] for s in portfolio])
cost
cost#> 37105.15
Collect (name, shares) tuples
= [ (s['name'], s['shares']) for s in portfolio ]
name_shares
name_shares#> [('IBM', 100), ('MSFT', 50), ('HPE', 75), ('CAT', 60), ('IBM', 200)]
8.1.5 Ex 2: Wrapping Function
def toint(x):
try:
return int(x)
except ValueError:
return None
= [ '1', '2', '-4', 'n/a', '-3', '5' ]
values
= [ toint(x) for x in values ]
data1 print(data1)
#> [1, 2, -4, None, -3, 5]
= [ toint(x) for x in values if toint(x) is not None ]
data2 print(data2)
#> [1, 2, -4, -3, 5]
Using :=
= [ v for x in values if (v:=toint(x)) is not None ]
data3 print(data3)
#> [1, 2, -4, -3, 5]
= [ v for x in values if (v:=toint(x)) is not None and v >= 0 ]
data4 print(data4)
#> [1, 2, 5]
8.2 Set & Dict Comprehension
8.2.1 Set Comprehension
A set of distinct values
# Set comprehension
= { s['name'] for s in portfolio }
names
names#> {'CAT', 'MSFT', 'IBM', 'HPE'}
8.2.2 Dict Comprehension
= { s['name']:s['price'] for s in portfolio }
prices
prices#> {'IBM': 95.25, 'MSFT': 45.67, 'HPE': 34.51, 'CAT': 67.89}
= {"a": "aa", "b": "bb"}
dd
for k, v in dd.items()} # Dict
{k: v #> {'a': 'aa', 'b': 'bb'}
for k in dd.keys()] # Keys
[k #> ['a', 'b']
for v in dd.values()] # Values
[v #> ['aa', 'bb']
8.3 map()
Function
map(function, iterable)
map()
applies a function to all the items in an input list. It returns a map object (an iterator) of the results after applying the given function to each item of the given iterable (list, tuple etc.).
= [1, 2, 3, 4, 5]
numbers = map(lambda x: x**2, numbers)
squares list(squares) # Converting the map object to a list
#> [1, 4, 9, 16, 25]
8.4 Differences and When to Use
- Readability: List comprehensions are often more readable and succinct, especially for simple operations or when filtering conditions are involved.
- Performance: Both list comprehensions and
map()
are faster than a for loop. However, for very large data sets,map()
can be more memory-efficient (since it returns an iterator), but the difference is often negligible. - Function Application: If you’re applying an existing function (especially a more complex one),
map()
can be more convenient. For operations that can be expressed as a single expression, or where you need conditional logic (like filtering), a list comprehension is often the better choice. - Return Type:
map()
returns a map object (iterator), which you often need to convert back to a list or another iterable type. List comprehensions return a list directly.
In summary, use list comprehensions when you need a readable and concise way to generate a new list by applying an expression to each item in an iterable, possibly with filtering. Use map()
when you need to apply an already-defined function to each item in an iterable and are okay with receiving an iterator as the result.