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.
In most of the cases you want to use list comprehensions, because other software developers will easily read your code.
If you want to compare speed, open the terminal and write:
$ python -mtimeit -s’x1=range(100)’ ‘map(hex, x1)’