-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_expressions.py
35 lines (22 loc) · 1.14 KB
/
lambda_expressions.py
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
# Lambda Expressions Example
# Lambda function inside the filter()
student_name = ['Marie', 'Bobi', 'John', 'Andy',
'Sally', 'Anthony', 'Willy', 'Aaron', 'Ross']
result = list(filter(lambda x: (x[0] == 'A'), student_name))
print(result)
# Match the username with their email using zip()
username = ['marie_coyman', 'bobi_garcia', 'john_smith', 'andy_tartakowsky',
'sally_simon', 'anthony_hopkins', 'willy_deniro', 'aaron_jason', 'ross_kane']
print(list(zip(username, email)))
# Square using Lambda function inside the map()
print(list(map(lambda num: num**2, [1, 2, 3])))
# Sort the tuples by having in mind second index
a = [(0, 2), (5, 2), (9, 9), (10, -1)]
a.sort(key=lambda x: x[1])
print(a)
# Find duplicates using Lambda function
some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = list(set([x for x in some_list if some_list.count(x) > 1]))
print(duplicates)