forked from SaeedIsa90/PythonExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_example.py
More file actions
40 lines (31 loc) · 942 Bytes
/
lambda_example.py
File metadata and controls
40 lines (31 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Lambda function
# lambda args: expression
# - any number of args
# - only one expression
# Default way to write a function
def my_add(x):
return x+1
my_add(1)
# 1 argument example:
my_func = lambda x: x + 1
print('Lambda add function', my_func(10))
# multiple arguments example
my_4_add = lambda a, b, c, d: a + b + c + d
res = my_4_add(1, 2, 3, 4)
print('Lambda add function with 4 arguments', res)
# List example: add 1 to each item
# without lambda:
def add_1(x):
return x + 1
lst = [1, 2, 3, 4]
new_lst = list(map(add_1, lst))
print('New list res', new_lst)
# with lambda function pointer
new_lst = list(map(my_func, lst))
print('New list with lambda res', new_lst)
# Using lambda directly in map function
new_lst = list(map(lambda x: x +1, lst))
print('New list with lambda res', new_lst)
# Lambda power example
new_lst = list(map(lambda x: x**2, lst))
print('New list - power one each element, new_lst', new_lst)