Maps one iterable onto another.
result = map(lambda x: x**2, range(7))
list(result)
a = range(7)
b = range(3, 10)
result = map(lambda x, y: y**2-x, a, b)
list(result)
These would be neater using list comprehensions.
print([x**2 for x in range(7)])
print([y**2-x for x, y in zip(range(7), range(3, 10))])
Or as generator expressions.
result_1 = (x**2 for x in range(7))
result_2 = (y**2-x for x, y in zip(range(7), range(3, 10)))
print(list(result_1))
print(list(result_2))
Thus map is a pretty redundant function.
Returns items for which the function evaluates True
.
result = filter(lambda x: x % 2, range(13))
list(result)
result = filter(lambda x: x > 3, range(13))
list(result)
You can map items and then filter the result.
result = filter(lambda x: x % 2, map(lambda x: x-11, range(21)))
list(result)