[function(i) for i in list1]
[i.function() for i in list1]
map(.function, list1) # error!
map(run_method(function), list1) # error!
from operator import methodcaller map(methodcaller('function'), list1)
methodcaller() accepts additional arguments that are then passed into the called method;
methodcaller('foo', 'bar', spam='eggs')(object) is the equivalent of
If all objects in
list1 are the same type or subclasses of that type, and the method you want to call doesn't take any arguments, you can pass in the unbound method to
map as the function to call. For example, to lowercase all strings in a list, you can use:
str.lower is the unbound method on the
Note that a list comprehension is not really the equivalent of a
map() can only do one loop, entirely in C.
zip() multiple iterable arguments, and
map() in Python 3 is itself an iterator.
A list comprehension on the other hand can do multiple (nested) loops and add in filtering, and the left-hand expression can be any valid Python expression including nested list comprehensions.