Map takes a function and applies it to every element of the collection. It then returns a new collection.
To get map() to return a list in Python 3.x, you need to cast it with list().
The method is roughly:
def map(func, iterable):
Given a simple list, we can apply a function to it:
This will output:
You can also use lambda with map:
Map isn’t unique to Python and you can achieve the same with list comprehensions.
[f(x) for x in iterable]
If you want to compare speed, open the terminal and write:
$ python -mtimeit -s'x1=range(100)' '[hex(x) for x in x1]' $ python -mtimeit -s'x1=range(100)' 'map(hex, x1)'