-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnested_list_notes_starter.py
More file actions
84 lines (52 loc) · 2.32 KB
/
nested_list_notes_starter.py
File metadata and controls
84 lines (52 loc) · 2.32 KB
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
75
76
77
78
79
80
81
82
83
84
"""
nested lists notes
this program continues to use the idea of a row of cars as a list
What are nested lists?
A nested list is a two-dimensional list, or a list containing
other lists or sequences. Think of like a matrix with rows and columns,
or in the case of a row of cars, a parking lot.
e.g.:
col1 col2 col3 col4
row1 ford toyota buick gm
row2 nissan lexus jeep ford
row3 bmw honda toyota gm
row4 tesla bmw jeep toyota
The more general term is a nested sequence (applies to tuples as well).
"""
#1 Creating a nested list (our parking lot)
lot = [
["ford", "toyota", "buick", "gm"],
["nissan", "lexus", "jeep", "ford"],
["bmw", "honda", "toyota", "gm"],
["tesla", "bmw", "jeep", "toyota"]
]
#2 Accessing nested elements (rows of cars)
#3 Accessing an individual element within a nested element
# On your own: display each of the following...
print("\nThe 1st car in row 4:", end="")
print("\nThe 2nd car in row 2:", end="")
print("\nAll cars in the 3rd column:")
print("\nAll cars in the 4th row with a tab inbetween them:")
# Challenge!
print("\nTry changing the first ford to an audi and then display it: ")
input("\nPress Enter to Continue...")
# Challenge!
print("\nTry printing the lot in column format with a single tab in between each car. \n \
Also, display row and column headings accordingly, without hardcoding:")
input("\nPress Enter to Continue...")
#4 Unpacking a sequence
# Unpacking a sequence means assigning each element
# its own variable in a single line of code.
# Another nested list example:
a_row = [["Ford", "Fusion"], ["Audi", "A4"], ["BMW", "3 Series"],["Jeep", "Wrangler"]]
#5 Appending a new sequence
#6 Sorting a nested sequence
print("\nRow of cars sorted by make:")
#7 Sorting a nested sequence by elements other than the first
# We have a new module that we need to use... operator (and the itemgetter function).
print("\nRow of cars sorted by model:")
#8 Reverse
print("\nRow of cars reverse sorted by model:")
#9 Searching in and the break keyword
# You have to loop through to find if an indvidual element is in a nested sequence
input("\nPress Enter to Exit")