Python Language map()


Example

map() is a builtin that is useful for applying a function to elements of an iterable. In Python 2, map returns a list. In Python 3, map returns a map object, which is a generator.

# Python 2.X
>>> map(str, [1, 2, 3, 4, 5])
['1', '2', '3', '4', '5']
>>> type(_)
>>> <class 'list'>

# Python 3.X
>>> map(str, [1, 2, 3, 4, 5])
<map object at 0x*>
>>> type(_)
<class 'map'>

# We need to apply map again because we "consumed" the previous map....
>>> map(str, [1, 2, 3, 4, 5])
>>> list(_)
['1', '2', '3', '4', '5']

In Python 2, you can pass None to serve as an identity function. This no longer works in Python 3.

Python 2.x2.3
>>> map(None, [0, 1, 2, 3, 0, 4])
[0, 1, 2, 3, 0, 4]
Python 3.x3.0
>>> list(map(None, [0, 1, 2, 3, 0, 5]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Moreover, when passing more than one iterable as argument in Python 2, map pads the shorter iterables with None (similar to itertools.izip_longest). In Python 3, iteration stops after the shortest iterable.

In Python 2:

Python 2.x2.3
>>> map(None, [1, 2, 3], [1, 2], [1, 2, 3, 4, 5])
[(1, 1, 1), (2, 2, 2), (3, None, 3), (None, None, 4), (None, None, 5)]

In Python 3:

Python 3.x3.0
>>> list(map(lambda x, y, z: (x, y, z), [1, 2, 3], [1, 2], [1, 2, 3, 4, 5]))
[(1, 1, 1), (2, 2, 2)]

# to obtain the same padding as in Python 2 use zip_longest from itertools
>>> import itertools
>>> list(itertools.zip_longest([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]))
[(1, 1, 1), (2, 2, 2), (3, None, 3), (None, None, 4), (None, None, 5)]

Note: instead of map consider using list comprehensions, which are Python 2/3 compatible. Replacing map(str, [1, 2, 3, 4, 5]):

>>> [str(i) for i in [1, 2, 3, 4, 5]]
['1', '2', '3', '4', '5']