2D Lists
Jessica Xu
Sept 30, 2024
CSCI 110 - Lecture 19
Today’s Agenda
|
|
| |
| |
| |
| |
Recap
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()
Mutability
mutable: once created, value can change
immutable: once created, value cannot change
immutable types:
mutable types:
Mutating a List
update: my_list[index] = value
add:
remove:
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
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?
2D Lists
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']]
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'
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'
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
Grids!
Candy Crush!
How to keep track of which symbol is in each cell of grid?
2D List: Candy Crush
[
['blue', 'red', 'yellow', 'blue'],
['purple', 'fish', 'yellow', 'yellow'],
['wall', 'orange', 'red', 'yellow']
]
Databases
Databases
database: collection of organized information to be easily accessed, managed, and/or updated
Internet Movie Database (IMDb)
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 |
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 |
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 |
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'],
]
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
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
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
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
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
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
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!
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!
2D Lists: Iterating
Just like lists, we can iterate over 2D lists!
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()
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()
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()
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()
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()
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
Nested Loops
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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
Questions?
Reminders
Reminder: Project 1 (pt. 5)
Reminder: HW 5
Reminder: Quiz 5
First 15 minutes of class this Friday!! Bring your laptops :)
Covered topics: For Loops, Lists
Reminder: Midterm Exam 1 Corrections
Reminder: STEP Applications are OPEN!
Grades!
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!