In Python 3.x, Python still has a rich set of functional tools built into: lists, generator expressions, iterators and generators, as well as functions like any() and all() that have a short circuit rating where it is perhaps.
Python's “Benevolent Dictator for Life” recreated the idea of removing map() because you can trivially reproduce its list effects:
lst2 = map(foo, lst) lst3 = [foo(x) for x in lst] lst2 == lst3
The Python lambda function has not been removed or renamed and will probably never be. However, it will probably never become more powerful. Python lambda limited to one expression; it cannot include statements and cannot include multiple lines of Python code.
Python plain-old-standard def defines a function object that can be passed as easily as a lambda object. You can even undo a function name after defining it if you really want to.
Example:
# NOT LEGAL PYTHON lst2 = map(lambda x: if foo(x): x**2; else: x, lst) # perfectly legal Python def lambda_function(x): if foo(x): return x**2 else: return x lst2 = map(lambda_function, lst) del(lambda_function) # can unbind the name if you wish
Note that you can actually use the “triple operator” in lambda , so the above example is a bit contrived.
lst2 = map(lambda x: x**2 if foo(x) else x, lst)
But some multi-line functions are hard to insert into lambda and are better handled like simple regular multi-line functions.
Python 3.x has not lost any functional power. There is a general feeling that lists and generator expressions are probably preferable to map() ; in particular, generator expressions can sometimes be used to execute the equivalent of a map() , but without highlighting the list and then releasing it. For instance:
total = sum(map(lst, foo)) total2 = sum(foo(x) for x in lst) assert total == total2
In Python 2.x, map() allocates a new list, which is summarized and immediately freed. The generator expression takes values one at a time and never links the memory of an entire list of values.
In Python 3.x, map() is "lazy," so both are equally effective. But as a result, in Python 3.x, the triple lambda example must be forced to expand into a list:
lst2 = list(map(lambda x: x**2 if foo(x) else x, lst))
It’s easier to just write a list comprehension!
lst2 = [x**2 if foo(x) else x for x in lst]