1 of 88

2D Lists

Jessica Xu

Sept 30, 2024

CSCI 110 - Lecture 19

2 of 88

Today’s Agenda

  • Recap (Lists)
  • 2D Lists!
  • Style Guide with the TAs (moved to Friday)
  • Quiz 5 (also moved to Friday)

3 of 88

Recap

4 of 88

Review: Lists

What are they?

Data structure containing ordered list of elements, which can be strings, numbers, other data types, and other data structures including lists

Is a data type, just like str, int, bool, float

Can len(), index, slice, in operator, concatenate, iterate

Why are they important?

Hold data that needs to stay in a certain order

Mutability

Change list using built-in list functions: append(), extend(), insert(), remove(), pop()

5 of 88

Mutability

mutable: once created, value can change

immutable: once created, value cannot change

immutable types:

  • int
  • float
  • bool
  • str

mutable types:

  • list
  • …?

6 of 88

Mutating a List

update: my_list[index] = value

add:

  • append(x) - add a value to the end of a list
  • insert(i, x) - add a value at index i in a list
  • extend(x) - add a list to the end of the list

remove:

  • pop(i) - removes (and returns) the value at index i
  • pop() - removes (and returns) the value at the end of the list
  • remove(x) - removes the first occurrence of the value x if found

7 of 88

Practice: Mutating lists

pokemon = ['Pikachu', 'Charmander', 'Squirtle', 'Bulbasaur']

pokemon[3] = 'Ivysaur'

pokemon.append('Pidgey')

pokemon.pop(0)

pokemon.pop()

pokemon.insert(2, 'Wartortle')

pokemon.remove('Squirtle')

pokemon.extend(['Charmeleon'])

pokemon.remove('Charizard')

['Pikachu', 'Charmander', 'Squirtle', 'Ivysaur']

['Charmander', 'Squirtle', 'Ivysaur', 'Pidgey']

['Pikachu', 'Charmander', 'Squirtle', 'Ivysaur', 'Pidgey']

['Charmander', 'Squirtle', 'Ivysaur']

['Charmander', 'Squirtle', 'Wartortle', 'Ivysaur']

['Charmander', 'Wartortle', 'Ivysaur']

['Charmander', 'Wartortle', 'Ivysaur', 'Charmeleon']

ValueError

8 of 88

Lists: Getting Info vs. Mutability

states = []

states.append("AL")

states.insert(len(states), "CA")

states.extend(["NY", "TN", "SC"])

states.pop()

states.remove(2)

states = ["AL", "NY", "TN", "CA"]

states[0]

states[1:4]

len(states)

states[1:2] + states[3:4]

"CA" in states

What is states after all of these?

9 of 88

2D Lists

10 of 88

Recall we can add multiple items to a list...

companies = []

companies.extend(['Google', 'Facebook'])

companies.extend(['Apple', 'Amazon', 'Zillow'])

companies.append(['Microsoft', 'Lyft'])

['Google', 'Facebook']

['Google', 'Facebook', 'Apple', 'Amazon', 'Zillow']

['Google', 'Facebook', 'Apple', 'Amazon', 'Zillow', ['Microsoft', 'Lyft']]

11 of 88

Let’s access items in our list...

companies =� ['Google', 'Facebook', 'Apple', 'Amazon', 'Zillow', ['Microsoft', 'Lyft']]

companies[5]

['Microsoft', 'Lyft']

companies[6]

IndexError

companies[5][0]

'Microsoft'

companies[5][1]

'Lyft'

companies[5][2]

IndexError

companies[5][1][2]

'f'

12 of 88

2D (2-Dimensional) Lists

A 2D list is a list of lists!

my_2d_list = [['a', 'b', 'c'], ['x', 'y', 'z']]

Can access inner list by indexing

my_2d_list[0] -> ['a', 'b', 'c']

Can access inner list values by indexing that list

my_2d_list[0][1] -> 'b'

13 of 88

What can we do with 2D lists?

Represent grids!

Lists of "outer" list each represents one row

Each value in "inner" lists represents value for a column

14 of 88

Grids!

15 of 88

Candy Crush!

How to keep track of which symbol is in each cell of grid?

16 of 88

2D List: Candy Crush

[

['blue', 'red', 'yellow', 'blue'],

['purple', 'fish', 'yellow', 'yellow'],

['wall', 'orange', 'red', 'yellow']

]

17 of 88

Databases

18 of 88

Databases

database: collection of organized information to be easily accessed, managed, and/or updated

19 of 88

Internet Movie Database (IMDb)

20 of 88

Database Records

Song

Artist

Album

Release Year

Genre

Circles

Post Malone

Hollywood's Bleeding

2019

Psychedelic pop

Ransom

Lil Tecca

We Love You Tecca

2019

Hip Hop/Rap

BOP

DaBaby

KIRK

2019

Hip Hop/Rap

Truth Hurts

Lizzo

Cuz I Love You (Deluxe)

2019

Pop

223's (feat. 9lokknine)

YNW Melly, 9lokknine

223's (feat. 9lokknine)

2019

Hip Hop/Rap

21 of 88

Database Records

one record

Song

Artist

Album

Release Year

Genre

Circles

Post Malone

Hollywood's Bleeding

2019

Psychedelic pop

Ransom

Lil Tecca

We Love You Tecca

2019

Hip Hop/Rap

BOP

DaBaby

KIRK

2019

Hip Hop/Rap

Truth Hurts

Lizzo

Cuz I Love You (Deluxe)

2019

Pop

223's (feat. 9lokknine)

YNW Melly, 9lokknine

223's (feat. 9lokknine)

2019

Hip Hop/Rap

22 of 88

Database Records

fields

Song

Artist

Album

Release Year

Genre

Circles

Post Malone

Hollywood's Bleeding

2019

Psychedelic pop

Ransom

Lil Tecca

We Love You Tecca

2019

Hip Hop/Rap

BOP

DaBaby

KIRK

2019

Hip Hop/Rap

Truth Hurts

Lizzo

Cuz I Love You (Deluxe)

2019

Pop

223's (feat. 9lokknine)

YNW Melly, 9lokknine

223's (feat. 9lokknine)

2019

Hip Hop/Rap

23 of 88

Database Records as Nested Lists

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

24 of 88

2D Lists: Indexing

Just like for lists, can index into 2D lists

All list functions work for 2D lists, because 2D lists are lists!

To index: first index “outer” list, then index into resulting “inner” list

25 of 88

2D Lists: Indexing

song = songs[3]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

26 of 88

2D Lists: Indexing

song = songs[3]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

27 of 88

2D Lists: Indexing

song = songs[3][0]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

28 of 88

2D Lists: Indexing

song = songs[0][3]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

29 of 88

2D Lists: Indexing

song = songs[4][2]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

30 of 88

2D Lists: Indexing

song = songs[5][2]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

IndexError!

31 of 88

2D Lists: Indexing

song = songs[1][4]

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

0

1

2

3

4

IndexError!

32 of 88

2D Lists: Iterating

Just like lists, we can iterate over 2D lists!

33 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

34 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

35 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

36 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

37 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

38 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Song: Circles

39 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

40 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Song: Circles

41 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Song: Circles

Artist: Post Malone

42 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Song: Circles

Artist: Post Malone

43 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Song: Circles

Artist: Post Malone

44 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Song: Circles

Artist: Post Malone

Year: 2019

45 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Song: Circles

Artist: Post Malone

Year: 2019

46 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Circles

Artist: Post Malone

Year: 2019

47 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

48 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

49 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

50 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

51 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Ransom

Artist: Lil Tecca

Year: 2019

Genre: Hip Hop/Rap

52 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Ransom

Artist: Lil Tecca

Year: 2019

Genre: Hip Hop/Rap

53 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Ransom

Artist: Lil Tecca

Year: 2019

Genre: Hip Hop/Rap

54 of 88

2D Lists: Iteration

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Song: Circles

Artist: Post Malone

Year: 2019

Genre: Psychedelic Pop

Song: Ransom

Artist: Lil Tecca

Year: 2019

Genre: Hip Hop/Rap

Song: BOP

Artist: DaBaby

Year: 2019

Genre: Hip Hop/Rap

55 of 88

What were we iterating through?

We were traversing the songs (rows in the database)!

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

AND for each row, we were traversing the values (columns in the database)!

SO, if we want to visit each thing in this 2D list, traverse it using nested for loops!

56 of 88

Nested Loops

57 of 88

Nested Loops!

A loop inside of a loop!

Usually use for loops but can be while loops

for i in range(4):

print(i)

for j in range(3):

print(str(i) + ' ' + str(j))

0�0 0

0 1

0 2

1

1 0

1 1

1 2

2

2 0

2 1

2 2

3

3 0

3 1

3 2

58 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

row

col

variables

59 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

0

row

col

variables

60 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

0

row

0

col

variables

61 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

0

row

0

col

variables

62 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

0

row

1

col

variables

63 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

0

row

1

col

variables

64 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

Nothing left in range!

2

3

0

row

1

col

variables

65 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

1

row

col

variables

2

3

66 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

1

row

0

col

variables

67 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

1

row

0

col

variables

68 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

1

row

1

col

variables

69 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

1

row

1

col

variables

70 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

Nothing left in range!

2

3

7

2

1

row

1

col

variables

71 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

2

row

col

variables

72 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

2

row

0

col

variables

73 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

8

2

row

0

col

variables

74 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

8

2

row

1

col

variables

75 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

8

5

2

row

1

col

variables

76 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

2

3

7

2

8

5

Nothing left in range!

2

row

1

col

variables

77 of 88

Tracing for loops

board = [[2, 3], [7, 2], [8, 5]]

for row in range(0, len(board)):

for col in range(0, len(board[row])):

print(board[row][col])

Nothing left in range!

2

3

7

2

8

5

2

row

1

col

variables

78 of 88

Practice: 2D Lists

songs = [

['Circles', 'Post Malone', 2019, 'Psychedelic Pop'],

['Ransom', 'Lil Tecca', 2019, 'Hip Hop/Rap'],

['BOP', 'DaBaby', 2019, 'Hip Hop/Rap'],

['Truth Hurts', 'Lizzo', 2019, 'Pop'],

['223\'s (feat. 9lokknine)', 'YNW Melly', 2019, 'Hip Hop/Rap'],

]

for song in songs:

print('Song: ' + song[0])

print('Artist: ' + song[1])

print('Year: ' + str(song[2]))

print('Genre: ' + song[3])

print()

Convert this to using a nested for loop!

79 of 88

Review: 2D Lists

What are they?

List of lists

Can use all list functions on 2D lists because they are lists

Can index and iterate through 2D lists to access stored data

Why are they important?

Holds data in particular order in which each item has many elements

databases: collection of organized information to be easily accessed, managed, and/or updated

80 of 88

Questions?

81 of 88

Reminders

82 of 88

Reminder: Project 1 (pt. 5)

  • MORE Blackjack!
  • Practice writing unit tests & integration tests :)
  • Due on Sunday (10/6) at 11:59pm

83 of 88

Reminder: HW 5

  • Due Wednesday @ 11:59 PM
  • This HW covers For Loops!

84 of 88

Reminder: Quiz 5

First 15 minutes of class this Friday!! Bring your laptops :)

Covered topics: For Loops, Lists

85 of 88

Reminder: Midterm Exam 1 Corrections

Due 10/8 @ 11:59pm!

Follow the instructions here

86 of 88

Reminder: STEP Applications are OPEN!

Applications due 10/25!

Apply here :)

87 of 88

Grades!

  • Go to https://csci110.page/grades/
    • Only midterms and projects so far, adding more soon!

88 of 88

Google 20% TAs

Missed the first few lectures of class? Need some extra support?

Book virtual OH with some folks from Google!

https://csci110.page/staff/#googler-teaching-assistants

If you feel that you are falling behind and want more 1:1 attention (or if you just want to talk to more Googlers), schedule a meeting with a Googer TA that fits your schedule!