Nested Dictionaries
Jake Shoudy
Oct 19, 2022
CSCI110 - Lecture 25
Announcements
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)
Project 2: Part 2
More search engine things using 2D Lists :)
Due Sunday October 30th at 11:59pm
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:
Quiz 7
Filters Hall of Fame!
- Adetoro Akinola
- Princess Nwanekwu
- Rupak Kunwar
- Michaela Robinson
- Keshav Shah
- Esther Ogundele
- Rohan Yadav
Recap
Tuples
Tuples are similar to lists but the biggest distinction is...
player = (“xxCOOLGUYxx”, True, 25)
To create a tuple, we use parentheses instead of brackets:
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
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'
Nested Dictionaries
Dictionaries
holidays = [
{'month': 10, 'day': 31},
{'month': 12, 'day': 25},
]
{'month': 10, 'day': 31}
{'month': 12, 'day': 25}
halloween = holidays[0]
christmas = holidays[1]
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?
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}
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))
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'
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'
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']
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']
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']
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']
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']
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']
Nested Dictionaries
Whenever we use a dictionary as a value within our dictionary
Generally useful for modeling hierarchical data
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’]
Let’s Code!
https://replit.com/team/csci110-01
Nested Dictionaries