reduce() functions bring a bit of functional programming to Python. All three of these are convenience functions that can be replaced with List comprehensions or loops but provide a more elegant and short-hand approach to some problems
map() function iterates through all items in the given iterable and executes the functions we passed as an argument on each of them.
def starts_with_A(s): return s == "A" fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"] map_object = map(starts_with_A, fruit) print(list(map_object))
[True, False, False, True, False]
As we can see, we ended up with a new list where the function
starts_with_A() was evaluated for each of the elements in the list fruit. The results of this function were added to the list sequentially.
The same can be done by the
fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"] map_object = map(lambda s : s == "A", fruit) print(list(map_object))