2D Lists
Jake Shoudy
Sept 28, 2022
CSCI 110 - Lecture 17
Announcements
STEP Application Open
Other freshman opportunities!
There are more opportunities than just Google! (Microsoft, Uber, Meta, Amazon, Capitol One, etc.)
https://csci110.org/mock-interviews/#internship-opportunities-for-freshmensophomores
GIR Midpoint Survey
HW6
Lists!
Due Friday (9/30) at 11:59pm
Lab6
Posted yesterday morning!
More lists practice!
Attempt before Friday at 11:59pm to count as Participation/Attendance for the week
Quiz 5 Scores
| Score |
Mean | 77% |
Min | 12% |
Max | 100% |
Median | 79% |
Percent of class above 70% | 67% |
Percent of class above 90% | 29% |
Recap
Review: Style Guide
What is it?
Standard in which programmers write code
Why is it important?
Makes code easy to read and organized
Everyone follows same standard, so when work together, know code will look a certain way
Python Style Guide
Variable and function names: all lowercase with underscores between words
Expressions and functions: exactly one space on either side of operator, parentheses only where necessary, no extra spaces
Review: Lists
What?
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, iterate, replace item, concat (using +)
List functions: append, extend, insert, remove, pop
Why?
Hold data that needs to stay in a certain order
Review: Mutability
ages = [3, 5, 8, 9]
ages.append(7)
ages = ages + [3, 4, 11]
ages.remove(3)
ages.insert(1, 10)
ages[2] = 12
ages.pop(1)
ages.extend([8, 2])
[3, 5, 8, 9, 7]
[3, 5, 8, 9, 7, 3, 4, 11]
[5, 8, 9, 7, 3, 4, 11]
[5, 10, 8, 9, 7, 3, 4, 11]
[5, 10, 12, 9, 7, 3, 4, 11]
[5, 12, 9, 7, 3, 4, 11]
[5, 12, 9, 7, 3, 4, 11, 8, 2]
Lists: Getting Items vs Mutability
states = []
states.append("AL")
states.insert(len(states), "CA")
states.extend(["NY", "TN"])
states.pop()
states.remove("CA")
states = ["AL", "NY", "TN", "CA"]
states[0]
states[1:3]
len(states)
states[1:2] + states[3:]
"CA" in states
2D Lists
Adding to lists
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']]
Accessing 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!
Ex: my_2d_list = [['a', 'b', 'c'], ['x', 'y', 'z']]
Can access inner list by indexing
Ex: my_2d_list[0] -> ['a', 'b', 'c']
Can access inner list values by indexing that list
Ex: 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
What can we do with 2D lists?
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 List: 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 List: Iterating
Just like for lists, can iterate over 2D lists
2D Lists: Iterating
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()
Let’s Code!
2D Lists
What?
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?
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?