1 of 35

Nested Dictionaries

Jake Shoudy

Oct 19, 2022

CSCI110 - Lecture 25

2 of 35

Announcements

3 of 35

HW8

Big O Practice and Coding problems using Sets

Can use dictionaries for coding problem 2 and 3.

Using just sets is extra credit

Due TOMORROW at 11:59pm! (Oct 20th)

4 of 35

Project 2: Part 2

More search engine things using 2D Lists :)

Due Sunday October 30th at 11:59pm

5 of 35

Exam 2: Wednesday Oct 26

Topics (up until end of last week):

Binary <-> Base 10

Data types

Operators (arithmetic, comparison, logical)

Variables

if / elif / else

while loops

Strings (length, index, slice)

Functions

Will include short answer & coding questions

For Loops

Lists

2D Lists

Big O

Sets

Dictionaries

How to prepare:

  • Review quizzes
  • Review homeworks
  • But also projects, labs, slides, replits

6 of 35

Quiz 7

  • Friday October 21st in the first 15 minutes of class
  • Meet in lab
  • Will cover:
    • Big O
    • Sets
    • Dictionaries

7 of 35

Filters Hall of Fame!

8 of 35

9 of 35

- Adetoro Akinola

10 of 35

- Princess Nwanekwu

11 of 35

- Rupak Kunwar

12 of 35

- Michaela Robinson

13 of 35

- Keshav Shah

14 of 35

- Esther Ogundele

15 of 35

- Rohan Yadav

16 of 35

Recap

17 of 35

Tuples

  • Immutable (therefore can be used as a key in a dictionary or in a set)
  • Can still index, iterate, get length, etc.

Tuples are similar to lists but the biggest distinction is...

player = (“xxCOOLGUYxx”, True, 25)

To create a tuple, we use parentheses instead of brackets:

18 of 35

Tuples as Keys

global_temps[(34.2384, -77.0118)]

global_temps = {

(38.8951, -77.0364): 74,

(32.1231, -71.3511): 61,

(34.2384, -77.0118): 55

}

55

19 of 35

Lists as Values of Dictionaries

songs = {

'Kendrick Lamar': ['N95', 'HUMBLE.'],

'Lizzo': ['Truth Hurts', 'Good As Hell'],

}

songs['Kendrick Lamar']

songs['Kendrick Lamar'][0]

songs['Kendrick Lamar'].append('Die Hard')

['N95', 'HUMBLE.']

'N95'

20 of 35

Nested Dictionaries

21 of 35

Dictionaries

holidays = [

{'month': 10, 'day': 31},

{'month': 12, 'day': 25},

]

{'month': 10, 'day': 31}

{'month': 12, 'day': 25}

halloween = holidays[0]

christmas = holidays[1]

22 of 35

Dictionaries

holidays = [

{'month': 10, 'day': 31},

{'month': 12, 'day': 25},

]

halloween = holidays[‘halloween’]

christmas = holidays[‘christmas’]

How to change holidays to be able to use ‘halloween’ and ‘christmas’ to index?

23 of 35

Nested Dictionaries

holidays = {

‘halloween’: {'month': 10, 'day': 31},

‘christmas’: {'month': 12, 'day': 25},

}

halloween_date = holidays[‘halloween’]

christmas_date = holidays[‘christmas’]

{'month': 10, 'day': 31}

{'month': 12, 'day': 25}

24 of 35

Nested Dictionaries: Iteration

holidays = {

'halloween': {'month': 10, 'day': 31},

'christmas': {'month': 12, 'day': 25},

}

for holiday in holidays:

month = holidays[holiday]['month']

day = holidays[holiday]['day']

print(holiday + ' is on ' + str(month) + '/' + str(day))

25 of 35

List of Dictionaries

songs = [

{'title': 'Circles', 'artist': 'Post Malone', 'genre': 'Pop'},

{'title': 'Ransom', 'artist': 'Lil Teccae', 'genre': 'Hip Hop/Rap'},

{'title': 'BOP', 'artist': 'DaBaby', 'genre': 'Hip Hop/Rap'},

{'title': 'Truth Hurts', 'artist': 'Lizzo', 'genre': 'Pop'},

{'title': '223\'s', 'artist': 'YNW Melly', 'genre': 'Hip Hop/Rap'},

]

print(songs[0]['artist'])

'Post Malone'

26 of 35

Nested Dictionaries

songs = {

'Circles': {'artist': 'Post Malone', 'genre': 'Pop'},

'Ransom': {'artist': 'Lil Teccae', 'genre': 'Hip Hop/Rap'},

'BOP': {'artist': 'DaBaby', 'genre': 'Hip Hop/Rap'},

'Truth Hurts': {'artist': 'Lizzo', 'genre': 'Pop'},

'223\'s': {'artist': 'YNW Melly', 'genre': 'Hip Hop/Rap'},

}

print(songs['Circles']['artist'])

'Post Malone'

27 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

28 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

29 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

30 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

31 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

32 of 35

Nested Dictionaries and Lists

songs = {

'Post Malone': [

{'title': 'Circles', 'year': 2019},

{'title': 'Goodbyes', 'year': 2019},

],

'Lizzo': [

{'title': 'Truth Hurts', 'year': 2019},

{'title': 'Good As Hell', 'year': 2016},

],

}

songs['Lizzo'][1]['year']

33 of 35

Nested Dictionaries

Whenever we use a dictionary as a value within our dictionary

Generally useful for modeling hierarchical data

34 of 35

Example: City population

population = {

'North America': {

'United States': {

...

'Washington': {...}

'Tennessee': {

'Nashville': 692587,

'Memphis': 650910,

'Chatanooga': 181370,

...

},

},

'Mexico': {...},

...

},

'Africa': {...},

...

}

population[‘North America’][‘United States’]

[‘Tennessee’][‘Nashville’]

35 of 35

Let’s Code!