AddisCoder: Week 2
Lecture 6B:
2-Dimensional Lists
2-Dimensional Lists
We learned about lists as an ordered collection of items of any type.
2-Dimensional Lists
We learned about lists as an ordered collection of items of any type.
2-Dimensional Lists
When you have a list that holds other lists, we call this a “2 dimensional list”
2-Dimensional Lists
When you have a list that holds other lists, we call this a “2 dimensional list”
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] |
2-Dimensional Lists
It’s useful to think of them as representing some sort of grid.
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] |
2-Dimensional Lists
It’s useful to think of them as representing some sort of grid.
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] print(seating[0]) |
,
> ['Anton', 'Jordan', 'Joy']
,
2-Dimensional Lists
It’s useful to think of them as representing some sort of grid.
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] print(seating[1]) |
,
,
> ['Ibrahim', 'Sam', 'Pooja']
2-Dimensional Lists
It’s useful to think of them as representing some sort of grid.
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] print(seating[2]) |
,
,
> ['Okoro', 'Nathan', 'Mira']
2-Dimensional Lists
To get a particular item in an inner list, we first index into the list to get the correct “row”, then index into that “row” to get the
item we want.
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] print(seating[0][1]) |
> Jordan
2-Dimensional Lists
Indexing into a list is an expression. So the outer index is applied to the value that the inner index returns.
seating[0][1] |
2-Dimensional Lists
seating[1][0] |
2-Dimensional Lists
seating[2][1] |
2-Dimensional Lists
seating[2][2] |
Nested For Loops
2D List Iterating
What if we want iterate over all of the seats?
seating = [ ['Anton', 'Jordan', 'Joy'], ['Ibrahim', 'Sam', 'Pooja'], ['Okoro', 'Nathan', 'Mira'] ] ???? |
> Anton
> Jordan
> Joy
> Ibrahim
> Sam
> Pooja
> Okoro
> Nathan
> Mira
Nested For Loops
For each row, we will for each name in that row print it to the screen.
Nested For Loops
For each row, we will for each name in that row print it to the screen.
??? |
Nested For Loops
For each row, we will for each name in that row print it to the screen.
# For each row index for row in range(3): # For this particular row, # print each entry ??? |
Nested For Loops
For each row, we will for each name in that row print it to the screen.
# For each row index for row in range(3): # For each column index for col in range(3): print(seats[row][col]) |
2D List Iterating
The other way to see this…
This is how we get all of the names:
seating[0][0]
seating[0][1]
seating[0][2]
seating[1][0]
seating[1][2]
seating[1][1]
seating[2][0]
seating[2][2]
seating[2][1]
2D List Iterating
Notice the pattern. Repeating [0,1,2] outside
seating[0][0]
seating[0][1]
seating[0][2]
seating[1][0]
seating[1][2]
seating[1][1]
seating[2][0]
seating[2][2]
seating[2][1]
2D List Iterating
Notice the pattern. Each repeated [0,1,2] is matched up with one digit (the row)
seating[0][0]
seating[0][1]
seating[0][2]
seating[1][0]
seating[1][2]
seating[1][1]
seating[2][0]
seating[2][2]
seating[2][1]
2D List Iterating
Notice the pattern. Each repeated [0,1,2] is matched up with one digit (the row)
This is the code that would generate all of these pairs of numbers together.
seating[0][0]
seating[0][1]
seating[0][2]
seating[1][0]
seating[1][2]
seating[1][1]
seating[2][0]
seating[2][2]
seating[2][1]
# For each row index for row in range(3): # For each column index for col in range(3): print(row, col) |