-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21thFilterinPython.py
More file actions
40 lines (20 loc) · 858 Bytes
/
21thFilterinPython.py
File metadata and controls
40 lines (20 loc) · 858 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
# ! Filter Function
# * If we want to remove some elements from an iterable, we can use do it in one line
# * We can still keep the iterable
m = [59, 78, 90, 43]
r = list(filter(lambda x: x >= 70, m)) # * We want elements which are above 70
print(r)
# * Checking if vowels exist in a given word
a = list('hello')
r = list(filter(lambda x : x in 'aeiou', a))
print(r)
# * To check if no.s are divisible by 3
a = [1, 2, 3, 4, 5, 6]
print(list(filter(lambda x:x%3==0,a)))
# * Example of using both map and filter
a = ['Ram', 'Tejas', 'Aditya', 'Ravi', 'Dinesh', 'Raghu']
print(list(map(str.upper, filter(lambda x : len(x) > 5, a))))
# * For readability, we can write the above code as
r = filter(lambda x:len(x)> 5, a) # * Names with length above 5 are used
rr = map(str.upper, r) # * Used to uppercase the names we got above
print(list(rr))