Apply a function to each iteration element and return the Results list.
From the documentation for map
int() tries to convert what is passed to an integer and raises a ValueError if you try something stupid, like this:
>>> int('Hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'Hello'
map() will return a list that has the return value of the function you ask it to call for any iterable. If your function returns nothing, you will get a list of None s, for example:
>>> def silly(x): ... pass ... >>> map(silly,'Hello') [None, None, None, None, None]
This is a short and efficient way to do something like this:
def verbose_map(some_function,something): results = [] for i in something: results.append(some_function(i)) return results
source share