1 of 25

AddisCoder: Week 2

Lecture 6B:

  • Nested Loops
  • Nested Lists
  • Slicing

2 of 25

2-Dimensional Lists

3 of 25

2-Dimensional Lists

We learned about lists as an ordered collection of items of any type.

4 of 25

2-Dimensional Lists

We learned about lists as an ordered collection of items of any type.

5 of 25

2-Dimensional Lists

When you have a list that holds other lists, we call this a “2 dimensional list”

6 of 25

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']

]

7 of 25

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']

]

8 of 25

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']

,

9 of 25

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']

10 of 25

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']

11 of 25

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

12 of 25

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]

13 of 25

2-Dimensional Lists

seating[1][0]

14 of 25

2-Dimensional Lists

seating[2][1]

15 of 25

2-Dimensional Lists

seating[2][2]

16 of 25

Nested For Loops

17 of 25

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

18 of 25

Nested For Loops

For each row, we will for each name in that row print it to the screen.

19 of 25

Nested For Loops

For each row, we will for each name in that row print it to the screen.

???

20 of 25

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

???

21 of 25

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])

22 of 25

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]

23 of 25

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]

24 of 25

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]

25 of 25

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)