-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap&Filter.py
More file actions
22 lines (18 loc) · 806 Bytes
/
Map&Filter.py
File metadata and controls
22 lines (18 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Map and Filter
# map() - takes a function and a sequence as arguments and applies the function to all the elements of the sequence,
# returning a list as the result
def product10(a,b):
return a * b
list1 = range(10)
list2 = range(5)
res = map(product10, list2, list1)
# result is [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]; applying the product10() function to
# each element of list1
print("First map of mult iterable", list(res))
# or...
res2 = map((lambda a: a * 10), list1) # result is [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] as well
print(list(res2))
# filter() - takes a function and a sequence as arguments and extracts all the elements in the list for which the
# function returns True
filt = filter(lambda a: a > 5, list1) # result is [6, 7, 8, 9]
print(list(filt))