map
transforms the items in a iterable in some way. The iterable is passed as the second argument and a function that performs the transformation as the first. So, map
calls the function on each of the items in the iterable producing a new one. It actually produces a generator, so list
there turns that generator into a list.The class in the example above adds nothing, other than a bit of noise.
>>> def double(x): ... return 2 * x ... >>> values = [1, 7, 4, 10, 9] >>> list(map(double, values)) [2, 14, 8, 20, 18]A list comprehension does the same:
>>> [double(v) for v in values] [2, 14, 8, 20, 18]