The map, zip, and filter functions
We'll start by reviewing map()
, filter()
, and zip()
, which are the main built-in functions you can employ when handling collections, and then we'll learn how to achieve the same results using two very important constructs: comprehensions and generators.
map
According to the official Python documentation:
map(function, iterable, …)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
We will explain the concept of yielding later on in the chapter. For now, let's translate this into code—we'll use a lambda
function that takes a variable number of positional arguments, and just returns them as a tuple:
# map.example.py
>>...