Working with Dictionaries of Dictionaries¶
In this exercise we're going to work with a 2D-list some dictionaries that you might recognize! It's the survey data from our first day.
I've taken the results of that survey and imported them into a 2D list a dictionary of dictionaries (with a little bit of clean-up).
The first row is a list of headers that describe each column. Every row after that is one of the entries that was submitted.
To access rows or entries within rows, you can use indexes.
Every entry in the dictionary represents one row in the original table. The key for each entry is the name of person.
To access entries you can use keys.
Examples¶
Access Mr. P's dictionary:
>> data["Mr. P"]
{
"name": "Mr. P",
"birthday": "4/26",
"from": "New Jersey",
"favorite_music": "LCD Soundsystem",
"travel_dest": "Brazil",
"favorite_movie": "The Truman Show",
"favorite_planet": "Venus",
"stay_age": 28,
"be_animal": "Cat",
"live_in": "Scadrial from Mistborn",
"fictional_pet": "War Horse from DnD",
"super_power": "Slow Time"
}
Access Mr. P's birthday:
data['Mr. P']['birthday']
'4/26'
Access what animal Mr. P would be:
data['Mr. P']['be_animal']
'Cat'
Loop over all of the participants entries in the dictionary:
for entry_name, entry_dict in data.items():
entry_be_animal = entry_dict["be_animal"]
print(f"{entry_name} would be a {entry_be_animal}")
Assignment¶
Use for-loops to do the following analysis:
- Count how many entries are from
"New York"
- Print the names of everyone who put
"France"
or"Paris"
as their travel destination. - Calculate the average
"stay_age"
Extra Challenge 1
4. Write a function that, given the data dictionary and a column name, returns the most popular answer in that column. If none are the most popular, return None
.
Extra Challenge 2 5. Write a function that converts a 2d-list like with a header row, like the one from our last assignment, into a nested-dictionary like the one in this assignment.
Extra Challege 3 6. With code, build an inverted dictionary where the top-level keys are the column names and the nested keys are the names.
invert_nested_dict(data)["travel_dest"]["Mr. P"] # => "Brazil"
data = {
"Mr. P": {
"name": "Mr. P",
"birthday": "4/26",
"from": "New Jersey",
"favorite_music": "LCD Soundsystem",
"travel_dest": "Brazil",
"favorite_movie": "The Truman Show",
"favorite_planet": "Venus",
"stay_age": 28,
"be_animal": "Cat",
"live_in": "Scadrial from Mistborn",
"fictional_pet": "War Horse from DnD",
"super_power": "Slow Time"
},
"Keitaro": {
"name": "Keitaro",
"birthday": "10/18",
"from": "Pennsylvania",
"favorite_music": "Motohiro Hata",
"travel_dest": "Australia",
"favorite_movie": "Cars 1",
"favorite_planet": "Earth",
"stay_age": 10,
"be_animal": "eagle",
"live_in": "Hogwarts",
"fictional_pet": "dragon",
"super_power": "Flight"
},
"Penelope": {
"name": "Penelope",
"birthday": "3/18",
"from": "New York",
"favorite_music": "Charli XCX",
"travel_dest": "France",
"favorite_movie": "La La Land",
"favorite_planet": "Earth",
"stay_age": 15,
"be_animal": "Manatee",
"live_in": "Idk",
"fictional_pet": "Boltund",
"super_power": "Flight"
},
"Ayaan": {
"name": "Ayaan",
"birthday": "1/19",
"from": "Virginia",
"favorite_music": "Daniel Caesar",
"travel_dest": "Tokyo",
"favorite_movie": "Into the Spider-Verse",
"favorite_planet": "Earth",
"stay_age": 25,
"be_animal": "Hawk",
"live_in": "Star Wars",
"fictional_pet": "The cat from Captain Marvel",
"super_power": "Invisibility"
},
"Ethan": {
"name": "Ethan",
"birthday": "3/9",
"from": "New York",
"favorite_music": "Lil Yachty",
"travel_dest": "Maldives",
"favorite_movie": "Shutter Island",
"favorite_planet": "Jupiter",
"stay_age": 6,
"be_animal": "Dog",
"live_in": "Bikini Bottom",
"fictional_pet": "Gary the Snail",
"super_power": "Teleportation"
},
"Jayden": {
"name": "Jayden",
"birthday": "7/27",
"from": "Florida",
"favorite_music": "Kendrick Lamar",
"travel_dest": "Dubai",
"favorite_movie": "War Dogs",
"favorite_planet": "Neptune",
"stay_age": 21,
"be_animal": "Stingray",
"live_in": "Hunger Games - Panem",
"fictional_pet": "Basilisk",
"super_power": "Invisibility"
},
"David": {
"name": "David",
"birthday": "11/17",
"from": "China",
"favorite_music": "Jay Chou",
"travel_dest": "Mars",
"favorite_movie": "don't have one",
"favorite_planet": "Earth",
"stay_age": 22,
"be_animal": "pigeon",
"live_in": "Swords and Magic",
"fictional_pet": "RedDragon",
"super_power": "Unlimited life"
},
"Daniel": {
"name": "Daniel",
"birthday": "5/30",
"from": "Canada",
"favorite_music": "Old Kanye West",
"travel_dest": "Japan",
"favorite_movie": "Interstellar",
"favorite_planet": "Saturn",
"stay_age": 25,
"be_animal": "Cat",
"live_in": "Pokemon",
"fictional_pet": "Phoenix",
"super_power": "Time Travel"
},
"Minhee": {
"name": "Minhee",
"birthday": "11/24",
"from": "South Korea",
"favorite_music": "The Weeknd",
"travel_dest": "Paris",
"favorite_movie": "Aladin",
"favorite_planet": "Earth",
"stay_age": 10,
"be_animal": "Owl",
"live_in": "Hogwarts",
"fictional_pet": "Jerry(mice)",
"super_power": "Time Travel"
},
"Christopher": {
"name": "Christopher",
"birthday": "4/23",
"from": "Taiwan",
"favorite_music": "Mac Miller",
"travel_dest": "Antarctica",
"favorite_movie": "Moneyball",
"favorite_planet": "Earth",
"stay_age": 23,
"be_animal": "A pig",
"live_in": "Star Wars",
"fictional_pet": "Ewok",
"super_power": "Freeze time"
},
"Claire": {
"name": "Claire",
"birthday": "6/5",
"from": "New York",
"favorite_music": "Ariana Grande",
"travel_dest": "Japan",
"favorite_movie": "Lord of the Rings",
"favorite_planet": "Earth",
"stay_age": 20,
"be_animal": "Dog",
"live_in": "any peaceful world",
"fictional_pet": "narwhale",
"super_power": "Flight"
},
"Jiya": {
"name": "Jiya",
"birthday": "8/23",
"from": "New Jersey",
"favorite_music": "My Chemical Romance",
"travel_dest": "Paris",
"favorite_movie": "X-Men",
"favorite_planet": "Earth",
"stay_age": 6,
"be_animal": "Butterfly",
"live_in": "the Grishaverse",
"fictional_pet": "Unicorn",
"super_power": "Invisibility"
},
"Yujie": {
"name": "Yujie",
"birthday": "3/25",
"from": "China",
"favorite_music": "EASon",
"travel_dest": "Australia",
"favorite_movie": "Marvel",
"favorite_planet": "Earth",
"stay_age": 18,
"be_animal": "whales",
"live_in": "magic",
"fictional_pet": "cats",
"super_power": "Invisibility"
},
"Florence": {
"name": "Florence",
"birthday": "7/11",
"from": "Indonesia",
"favorite_music": "Bruno Mars",
"travel_dest": "Italy",
"favorite_movie": "any mission impossible movies",
"favorite_planet": "Sun",
"stay_age": 19,
"be_animal": "poodle dog",
"live_in": "marvel",
"fictional_pet": "moana's rooster",
"super_power": "Teleportation "
},
"Kevin": {
"name": "Kevin",
"birthday": "12/9",
"from": "China",
"favorite_music": "Coldplay",
"travel_dest": "The Maldives",
"favorite_movie": "Dune 2",
"favorite_planet": "Jupiter",
"stay_age": 25,
"be_animal": "Orca",
"live_in": "The matrix",
"fictional_pet": "The sand worm",
"super_power": "Telepathy"
},
"Tony": {
"name": "Tony",
"birthday": "10/17",
"from": "China",
"favorite_music": "Hatsune Miku",
"travel_dest": "Andromeda",
"favorite_movie": "Interstellar",
"favorite_planet": "Saturn",
"stay_age": 18,
"be_animal": "bird",
"live_in": "Idk",
"fictional_pet": "Dragon",
"super_power": "Time stop"
},
"Deniz": {
"name": "Deniz",
"birthday": "4/10",
"from": "Turkey",
"favorite_music": "Coldplay",
"travel_dest": "Japan",
"favorite_movie": "Grown Ups",
"favorite_planet": "Jupiter",
"stay_age": 24,
"be_animal": "Cat",
"live_in": "StarWars",
"fictional_pet": "DrGON",
"super_power": "Flight"
}
}
my_dict = {"a": 1, "b": 2, "c": 3}
for k, v in my_dict.items():
print(k, v)
a 1 b 2 c 3