1 of 41

2D Lists

Jake Shoudy

Sept 28, 2022

CSCI 110 - Lecture 17

2 of 41

Announcements

3 of 41

STEP Application Open

Apply early! Before end of the week

Freshman application

Sophomore application

4 of 41

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

5 of 41

GIR Midpoint Survey

  • Half way through!
  • Anonymous feedback for my bosses back at Google
  • Important to understand how effective our program is!
  • PLEASE fill this out

https://google.qualtrics.com/jfe/form/SV_cBiIyaid3kkhtSS

6 of 41

HW6

Lists!

Due Friday (9/30) at 11:59pm

7 of 41

Lab6

Posted yesterday morning!

More lists practice!

Attempt before Friday at 11:59pm to count as Participation/Attendance for the week

8 of 41

Quiz 5 Scores

Score

Mean

77%

Min

12%

Max

100%

Median

79%

Percent of class above 70%

67%

Percent of class above 90%

29%

9 of 41

Recap

10 of 41

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

11 of 41

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

12 of 41

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]

13 of 41

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

14 of 41

2D Lists

15 of 41

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

16 of 41

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'

17 of 41

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'

18 of 41

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

19 of 41

What can we do with 2D lists?

20 of 41

Candy Crush!

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

21 of 41

2D List: Candy Crush

[

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

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

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

]

22 of 41

Databases

23 of 41

Databases

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

24 of 41

Internet Movie Database (IMDb)

25 of 41

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

26 of 41

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

27 of 41

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

28 of 41

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

]

29 of 41

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

30 of 41

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

31 of 41

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

32 of 41

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

33 of 41

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

34 of 41

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

35 of 41

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!

36 of 41

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!

37 of 41

2D List: Iterating

Just like for lists, can iterate over 2D lists

38 of 41

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

39 of 41

Let’s Code!

40 of 41

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

41 of 41

Questions?