forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_comprehensions.py
42 lines (32 loc) · 1.14 KB
/
list_comprehensions.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
# list comprehensions
# basic format: new_list = [transform sequence [filter] ]
import random
under_10 = [x for x in range(10)]
print('under_10: ' + str(under_10))
squares = [x**2 for x in under_10]
print('squares: ' + str(squares))
odds = [x for x in range(10) if x%2 == 1]
print('odds: ' + str(odds))
ten_x = [x * 10 for x in range(10)]
print('ten_x: ' + str(ten_x))
# get all numbers from a string
s = 'I love 2 go t0 the store 7 times a w3ek.'
nums = [x for x in s if x.isnumeric()]
print('nums: ' + ''.join(nums))
# get index of a list item
names = ['Cosmo', 'Pedro', 'Anu', 'Ray']
idx = [k for k, v in enumerate(names) if v == 'Anu']
print('index = ' + str(idx[0]))
# delete an item from a list
letters = [x for x in 'ABCDEF']
random.shuffle(letters)
letrs = [a for a in letters if a != 'C']
print(letters, letrs)
# if-else condition in a comprehension (must come before iteration)
nums = [5, 3, 10, 18, 6, 7]
new_list = [x if x%2 == 0 else 10*x for x in nums]
print('new list: ' + str(new_list))
# nested loop iteration for 2D list
a = [[1,2],[3,4]]
new_list = [x for b in a for x in b]
print(new_list)