-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26_Python_List_Comprehension.py
69 lines (53 loc) · 1.44 KB
/
26_Python_List_Comprehension.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Python - List Comprehension
print("Python - List Comprehension:")
# Without using list comprehension
print("\nWithout using list comprehension:")
list_1 = ['apple', 'mango', 'banana', 'malta', 'cherry']
list_2 = []
for x in list_1:
if 'm' in x:
list_2.append(x)
print(list_2)
# Using list comprehension
print("\nUsing list comprehension:")
list_3 = ['apple', 'mango', 'banana', 'malta', 'cherry']
list_4 = [i for i in list_3 if 'm' in i]
print(list_4)
# Another example of using list comprehension
print("\nAnother example of using list comprehension:")
list_5 = [
'apple', 'apple', 'banana', 'malta', 'apple', 'orange', 'cherry', 'mango',
'apple'
]
list_6 = [j for j in list_5 if j == 'apple']
list_7 = [k for k in list_5 if k != 'apple']
print(list_6)
print(list_7)
# With no if statement
print("\nWith no if statement:")
list_8 = [l for l in list_5]
print(list_8)
# Iterable
print("\nIterable:")
print("Example 1:")
list_9 = [m for m in range(10)]
print(list_9)
print("Example 2:")
list_10 = [n for n in range(10) if n < 5]
print(list_10)
# Expression
print("\nExpression:")
# Example 1
print("Example 1:")
list_11 = ['toyota','mazda','nissan']
list_12 = [o.upper() for o in list_11]
print(list_12)
# Example 2
print("Example 2:")
list_13 = ['hi' for p in list_11]
print(list_13)
# Example 3
print("Example 3:")
list_14 = ['apple','banana','mango','cherry','banana']
list_15 = [q if q != 'banana' else 'orange' for q in list_14]
print(list_15)