-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_in_python.py
More file actions
31 lines (17 loc) · 797 Bytes
/
map_in_python.py
File metadata and controls
31 lines (17 loc) · 797 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
# map(function_object, iterable1, iterable2,...)
# map functions expects a function object and any number of iterables like list, dictionary, etc.
# It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.
def multiply(x):
return x + x
def int_fn(x):
return x
print map(int_fn, [9,9])
print map(multiply, [1,2,3,4])
print map(lambda x : x*x, [1,2,3,4])
dict_a = [{'name': 'python', 'points': 10}, {'name': 'java', 'points': 8}]
map(lambda x : x['name'], dict_a) # Output: ['python', 'java']
map(lambda x : x['points']*10, dict_a) # Output: [100, 80]
map(lambda x : x['name'] == "python", dict_a) # Output: [True, False]
list_a = [1,2,3]
list_b = [4,5,6]
print map(lambda x,y : x + y , list_a, list_b)