Skip to content

Commit d04828f

Browse files
committed
python fancy func
1 parent 9f53cab commit d04828f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Misc/Lambda,map,reduce,filter.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''lambda is a one line anonymous function '''
2+
func = lambda x,y: x if x>y else y #function to return max of two numbers
3+
print(func(2,7))
4+
#7
5+
6+
7+
from functools import reduce
8+
lis = [1,2,3,4]
9+
print(reduce(lambda x,y:x+y,lis)) #multiplying all elements of a list using FILTER
10+
#10
11+
12+
13+
print(list(map(lambda x: x**2, lis))) #squaring all elements of a list using MAP
14+
#[1, 4, 9, 16]
15+
16+
17+
18+
print(list(filter(lambda x: x>10,lis))) #filtering elements of a list using FILTER
19+
#[] - empty list
20+
21+
22+
'''
23+
24+
In filter if you guessed 16 to be answer, you are wrong.
25+
These functions dont modify the input lists but create new ones
26+
27+
'''

0 commit comments

Comments
 (0)