Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0be7a3

Browse files
added lambda function program in filter, map
1 parent d0425b9 commit a0be7a3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# Lambda Functions is also known as Anonymous function bcoz they don't have any name some tym they also called oneline function bcoz they written in oneline of code
3+
# function returning from function thats where lambda function are used
4+
5+
from functools import reduce
6+
7+
# def double(x):
8+
# return x*2
9+
#
10+
# def add(x,y):
11+
# return x+y
12+
#
13+
# def pro(x,y,z):
14+
# return x*y*z
15+
16+
17+
# Lambda function of above functions
18+
19+
double = lambda x : x * 2
20+
add = lambda x, y : x + y
21+
pro = lambda x, y, z : x * y * z
22+
23+
print(double(10))
24+
print(add(10,20))
25+
print(pro(10,20,30))
26+
27+
# map, filter, reduce
28+
29+
my_list1 = [2,5,8,10,9,3]
30+
my_list2 = [1,4,7,8,5,1]
31+
32+
a = map(lambda x : x * 2, my_list1)
33+
print(list(a)) # u must to cast list otherwise it will give wierd hexadecimal instead of list
34+
35+
b = map(lambda x, y : x + y, my_list1 , my_list2)
36+
print(list(b))
37+
38+
# filter function gives boolean value and it take function as 1st argument
39+
40+
c= filter(lambda x : x%2 ==0, my_list1)
41+
print(list(c))
42+
43+
d= filter(lambda x : True if x > 5 else False, my_list1) # using if else inside filter
44+
print(list(d))
45+
46+
# to use reduce we have import functool at the top
47+
48+
e = reduce(lambda x, y : x+y, my_list1)
49+
print(e)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /