Python lambda keyword

Python lambda keyword creates an anonymous function at runtime. Anonymous function sometimes is more convenient when used with itertools or enclosed in another function.

>>> f = lambda x: x%2
>>> f(3)
1
>>> f(20)
0
The above code is equivalent to:
>>> def f(x): return x%2;

Combine lambda with itertools is very convenient:
>>> from itertools import *
>>> data = [2,6,9,3,1,5]
>>> x = filter(lambda x: x%2, data)
>>> for i in x: print (i)
9
3
1
5
















endmemo.com © 2024  | Terms of Use | Privacy | Home