When using map() you sometimes can be fooled by Pythons lazy evaluation. Many functions returning complex or iterable data don’t do this directly but return a generator object, which when iterated over, yields the result values.
But sometimes you will need the result set at once. For example when map()ing a list one would sometimes coerce Python to return the whole resulting list. This can be done by applying the list() function to the generator like this:
l=[1,2,3] l1=map(lambda x: x+1, l) print(l1) <map object at 0x10f4536d8> l1=map(lambda x: x+1, l) list(l1) [2, 3, 4]
In line 5 I have to recreate the map object since print() seems to empty it.
When applying a standard function with map() it’s needed to qualify the module path on call:
l=["Hello ", " World"] l1=map(strip, l) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'strip' is not defined
In this case it’s the str module:
l1=map(str.strip, l) list(l1) ['Hello', 'World']
Thats all for now. Have fun.