-
Notifications
You must be signed in to change notification settings - Fork 1
/
16-List_nested.py
74 lines (59 loc) · 2.28 KB
/
16-List_nested.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
70
71
72
73
74
'''
author : Jaydatt
Nested List and access or copy
'''
print("-------------- nested-0 -----------------")
# different data type in list.
nested1 = [1, 2, ['a', 'b', 'c'],['d', 'e'],['f', 'g', 'h']]
for x in nested1:
print("level1: ")
if type(x) is list:
for y in x:
print(" level2: {}".format(y))
else:
print(x)
print("-------------- nested-1 -----------------")
print([10, 20, 30][1])
nested1 = [['a', 'b', 'c'],['d', 'e'],['f', 'g', 'h']]
y = nested1[1]
print(y)
print(y[0])
print(nested1[1][0])
print("-------------- nested-2 -----------------")
info = {'personal_data':
{'name': 'Lauren',
'age': 20,
'major': 'Information Science',
'physical_features':
{'color': {'eye': 'blue',
'hair': 'brown'},
'height': "5'8"}
},
'other':
{'favorite_colors': ['purple', 'green', 'blue'],
'interested_in': ['social media', 'intellectual property', 'copyright', 'music', 'books']
}
}
color = info['personal_data']['physical_features']['color']
print(color)
print("-------------- nested-3 -----------------")
nested1 = [['a', 'b', 'c'],['d', 'e'],['f', 'g', 'h']]
for x in nested1:
print("level1: ")
for y in x:
print(" level2: " + y)
print("-------------- nested-4 -----------------")
# Below, we have provided a list of lists that contain information about people. Write code to create a new list that contains every person’s last name, and save that list as last_names.
info = [['Tina', 'Turner', 1939, 'singer'], ['Matt', 'Damon', 1970, 'actor'], ['Kristen', 'Wiig', 1973, 'comedian'], ['Michael', 'Phelps', 1985, 'swimmer'], ['Barack', 'Obama', 1961, 'president']]
last_names = []
for i in info:
last_names += [i[1]]
print("last name: ",last_names)
print("-------------- nested-5 -----------------")
# Below, we have provided a list of lists named L. Use nested iteration to save every string containing “b” into a new list named b_strings.
L = [['apples', 'bananas', 'oranges', 'blueberries', 'lemons'], ['carrots', 'peas', 'cucumbers', 'green beans'], ['root beer', 'smoothies', 'cranberry juice']]
b_strings = []
for lst in L:
for word in lst:
if 'b' in word:
b_strings += [word]