Traversing Through Dictionaries in Python

In this challenge, you are given a large dictionary show_data.py. Your job is to navigate through this dictionary and answer the prompts via key-value pairs.

Here’s an example:

Prompt: What actor plays Ned Stark in Game of Thrones (drama)?

Correct Answer:

shows["genre"]["drama"]["game_of_thrones"]["cast"][0]["actor"]

Starter file

# Use the `from` keyword to import the `shows` dictionary from the `show_data.py` file


# QUESTION 1: Who is the actor that plays Squidward in Spongebob (kids)?


# QUESTION 2: Patrick Warburton plays Joe Swanson in Family Guy (comedy). What is the link to his imdb page?


# QUESTION 3: Is the Walking Dead still running?


# QUESTION 4: Who plays Dexter in Dexter (drama) and who plays Dexter in Dexter's Lab (kids)?
# HINT: You can print multiple items at once by using a comma like this: print(thing1, thing2)


# QUESTION 5: Who are the creators of Stranger Things (drama)?


# QUESTION 6: Who hosts the Daily Show (talk)?


# QUESTION 7: Who are all the hosts of the view (talk)
# Hint: You will need to use a loop for this one. You may not simply log the entire list, but must log each name individually


# QUESTION 8: What are the show names of the Impractical Jokers (comedy)
# Hint: You will need to use a loop for this one. You may not simply log the entire list, but must log each name individually


# QUESTION 9: Who does Will Arnett play in Arrested Development (comedy)


# QUESTION 10: Who plays Yami Yugi in Yu-Gi-Oh (kids)?


# QUESTION 11: How many seasons did the Office (comedy) run?


# QUESTION 12: Who are the main characters of the Office (comedy) (not the actors, but the actual character names)?


# QUESTION 13: List the characters in Teen Titans (kids)


# QUESTION 14: What is the link to the IMDB page for the actor who plays Mr. Krabs (Spongebob, kids)?


# QUESTION 15: Who plays Negan in The Walking Dead?


# QUESTION 16: List the main cast of Dexter (drama) (the actors, not the characters)


# QUESTION 17: Is Game of Thrones(drama) still running?


# QUESTION 18: Who does Peter Dinklage play in Game of Thrones (drama)?


# QUESTION 19: List the American Idol Judges


# QUESTION 20: Who plays Dustin in Stanger Things (drama)?

Instructions

Open the starter file and perform the following:

  1. Use the from keyword to import the shows dictionary from the show_data.py file.
  2. Answer each question by printing the expected result using bracket notation for the show dictionary.

Hint

In this activity, you may find that you’re repeating yourself a lot. It may be helpful to create variables that point to different locations within the dictionary. For example:

drama = shows["genre"]["drama"]

Then, to get the actor who plays Rick in The Walking Dead, we would simply write:

drama["the_walking_dead"]["cast"][0]["actor"]

As opposed to:

shows["genre"]["drama"]["the_walking_dead"]["cast"][0]["actor"].

Solution

# Use the `from` keyword to import the `shows` dictionary from the `show_data.py` file
from show_data import shows

# QUESTION 1: Who is the actor that plays Squidward in Spongebob (kids)?
print(shows['genre']['kids']['Spongebob']['cast'][3]['actor'])

# QUESTION 2: Patrick Warburton plays Joe Swanson in Family Guy (comedy). What is the link to his imdb page?
print(shows['genre']['comedy']['family_guy']['cast'][4]['imdb'])

# QUESTION 3: Is the Walking Dead still running?
print(shows['genre']['drama']['the_walking_dead']['still_running'])

# QUESTION 4: Who plays Dexter in Dexter (drama) and who plays Dexter in Dexter's Lab (kids)?
# HINT: You can print multiple items at once by using a comma like this: print(thing1, thing2)
print(shows['genre']['drama']['dexter']['cast'][0]['actor'], shows['genre']['kids']['dexters_lab']['cast'][0]['actor'])

# QUESTION 5: Who are the creators of Stranger Things (drama)?
print(shows['genre']['drama']['stranger_things']['creators'])

# QUESTION 6: Who hosts the Daily Show (talk)?
print(shows['genre']['talk']['the_daily_show']['host'])

# QUESTION 7: Who are all the hosts of the view (talk)
# Hint: You will need to use a loop for this one. You may not simply log the entire list, but must log each name individually
for person in shows['genre']['talk']['the_view']['host']:
    print(person)

# QUESTION 8: What are the show names of the Impractical Jokers (comedy)
# Hint: You will need to use a loop for this one. You may not simply log the entire list, but must log each name individually
for character in shows['genre']['comedy']['impractical_jokers']['cast']:
    print(character['showName'])

# QUESTION 9: Who does Will Arnett play in Arrested Development (comedy)
print(shows['genre']['comedy']['arrested_development']['cast'][2]['character'])

# QUESTION 10: Who plays Yami Yugi in Yu-Gi-Oh (kids)?
print(shows['genre']['kids']['yu_gi_oh']['cast'][0]['actor'])

# QUESTION 11: How many seasons did the Office (comedy) run?
print(shows['genre']['comedy']['the_office']['num_seasons'])

# QUESTION 12: Who are the main characters of the Office (comedy) (not the actors, but the actual character names)?
for character in shows['genre']['comedy']['the_office']['cast']:
    print(character['character'])

# QUESTION 13: List the characters in Teen Titans (kids)
for character in shows['genre']['kids']['teen_titans']['cast']:
    print(character['character'])

# QUESTION 14: What is the link to the IMDB page for the actor who plays Mr. Krabs (Spongebob, kids)?
print(shows['genre']['kids']['Spongebob']['cast'][1]['imdb'])

# QUESTION 15: Who plays Negan in The Walking Dead?
print(shows['genre']['drama']['the_walking_dead']['cast'][2]['actor'])

# QUESTION 16: List the main cast of Dexter (drama) (the actors, not the characters)
for character in shows['genre']['drama']['dexter']['cast']:
    print(character['actor'])

# QUESTION 17: Is Game of Thrones(drama) still running?
print(shows['genre']['drama']['game_of_thrones']['still_running'])

# QUESTION 18: Who does Peter Dinklage play in Game of Thrones (drama)?
print(shows['genre']['drama']['game_of_thrones']['cast'][1]['character'])

# QUESTION 19: List the American Idol Judges
for judge in shows['genre']['reality']['american_idol']['judges']:
    print(judge)

# QUESTION 20: Who plays Dustin in Stanger Things (drama)?
print(shows['genre']['drama']['stranger_things']['cast'][3]['actor'])

Show data

shows = {
    "genre": {
        "drama": {
            "game_of_thrones": {
                "cast": [
                    {
                        "actor": "Sean Bean",
                        "character": "Ned Stark",
                        "imdb": "https://www.imdb.com/name/nm0000293/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Peter Dinklage",
                        "character": "Tyrion Lannister",
                        "imdb": "https://www.imdb.com/name/nm0227759/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Maisie Williams",
                        "character": "Arya Stark",
                        "imdb": "https://www.imdb.com/name/nm3586035/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Emilia Clarke",
                        "character": "Danaerys Targaryen",
                        "imdb": "https://www.imdb.com/name/nm3592338/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Kit Harrington",
                        "character": "Jon Snow",
                        "imdb": "https://www.imdb.com/name/nm3229685/?ref_=nv_sr_1"
                    },
                ],
                "num_seasons": 8,
                "creators": ["David Benioff", "D.B. Weiss"],
                "still_running": False
            },
            "mad_men": {
                "cast": [
                    {
                        "actor": "Jon Hamm",
                        "character": "Don Draper",
                        "imdb": "https://www.imdb.com/name/nm0358316/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Christina Hendricks",
                        "character": "Joan Holloway",
                        "imdb": "https://www.imdb.com/name/nm0376716/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "January Jones",
                        "character": "Betty Draper",
                        "imdb": "https://www.imdb.com/name/nm0005064/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Elizabeth Moss",
                        "character": "Peggy Olson",
                        "imdb": "https://www.imdb.com/name/nm0005253/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "John Slattery",
                        "character": "Roger Sterling",
                        "imdb": "https://www.imdb.com/name/nm0805476/?ref_=nv_sr_1"
                    }
                ],
                "num_seasons": 5,
                "creators": "Matthew Weiner",
                "still_running": False
            },
            "dexter": {
                "cast": [
                    {
                        "actor": "Michael C. Hall",
                        "character": "Dexter Morgan",
                        "imdb": "https://www.imdb.com/name/nm0355910/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jennifer Carpenter",
                        "character": "Deb Morgan",
                        "imdb": "https://www.imdb.com/name/nm1358539/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "David Zayas",
                        "character": "Angel Batista",
                        "imdb": "https://www.imdb.com/name/nm0953882/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Desmond Harrington",
                        "character": "Joey Quinn",
                        "imdb": "https://www.imdb.com/name/nm0004993/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "James Remar",
                        "character": "Harry Morgan",
                        "imdb": "https://www.imdb.com/name/nm0001664/?ref_=nv_sr_1"
                    }
                ],
                "num_seasons": 8,
                "creators": "James Manos Jr.",
                "still_running": False
            },
            "stranger_things": {
                "cast": [
                    {
                        "actor": "Millie Bobby Brown",
                        "character": "Eleven",
                        "imdb": "https://www.imdb.com/name/nm5611121/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "David harbour",
                        "character": "Chief Hopper",
                        "imdb": "https://www.imdb.com/name/nm1092086/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Winona Ryder",
                        "character": "Joyce",
                        "imdb": "https://www.imdb.com/name/nm0000213/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Gaten Matarazzo",
                        "character": "Dustin",
                        "imdb": "https://www.imdb.com/name/nm7140802/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Finn Wolfhard",
                        "character": "Mike",
                        "imdb": "https://www.imdb.com/name/nm6016511/?ref_=nv_sr_1"
                    }
                ],
                "num_seasons": 2,
                "creators": ["Ross Duffer", "Matt Duffer"],
                "still_running": True
            },
           "the_walking_dead": {
                "cast": [
                    {
                        "actor": "Andrew Lincoln",
                        "character": "Rick",
                        "imdb": "https://www.imdb.com/name/nm0511088/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Danai Gurira",
                        "character": "Michonne",
                        "imdb": "https://www.imdb.com/name/nm1775091/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jeffrey Dean Morgan",
                        "character": "Negan",
                        "imdb": "https://www.imdb.com/name/nm0604742/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Norman Reedus",
                        "character": "Daryl",
                        "imdb": "https://www.imdb.com/name/nm0005342/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Steven Yeun",
                        "character": "Glenn",
                        "imdb": "https://www.imdb.com/name/nm3081796/?ref_=nv_sr_1"
                    }
                ],
                "num_seasons": 8,
                "creators": ["Robert Kirkman", "Frank Darabont"],
                "still_running": True
            }
        },

        "comedy": {
            "the_office": {
                "cast": [
                    {
                        "actor": "Steve Carell",
                        "character": "Michael Scott",
                        "imdb": "https://www.imdb.com/name/nm0136797/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jim Halpert",
                        "character": "John Krasinsky",
                        "imdb": "https://www.imdb.com/name/nm1024677/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Rainn Wilson",
                        "character": "Dwight Schrute",
                        "imdb": "https://www.imdb.com/name/nm0933988/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jenna Fischer",
                        "character": "Pam Beasley",
                        "imdb": "https://www.imdb.com/name/nm0278979/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Mindy Kaling",
                        "character": "Kelly Kapoor",
                        "imdb": "https://www.imdb.com/name/nm1411676/?ref_=nv_sr_1"
                    }
                ],
                "num_seasons": 9,
                "creators": ["Ricky Gervais", "Stephen Merchant", "Greg Daniels"],
                "still_running": False
            },
            "parks_and_recreation": {
                "cast": [
                    {
                        "actor": "Amy Poehler",
                        "character": "Leslie Knope",
                        "imdb": "https://www.imdb.com/name/nm0688132/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Nick Offerman",
                        "character": "Ron Swanson",
                        "imdb": "https://www.imdb.com/name/nm0644406/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Chris Pratt",
                        "character": "Andy Dwyer",
                        "imdb": "https://www.imdb.com/name/nm0695435/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Aziz Ansari",
                        "character": "Tom Haverford",
                        "imdb": "https://www.imdb.com/name/nm2106637/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Aubrey Plaza",
                        "character": "April Ludgate",
                        "imdb": "https://www.imdb.com/name/nm2201555/?ref_=nv_sr_1"
                    }
                ]
            },
            "arrested_development": {
                "cast": [
                    {
                        "actor": "Jason Bateman",
                        "character": "Michael Bluth",
                        "imdb": "https://www.imdb.com/name/nm0000867/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jeffrey Tambor",
                        "character": "Oscar Bluth",
                        "imdb": "https://www.imdb.com/name/nm0001787/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Will Arnett",
                        "character": "Gob Bluth",
                        "imdb": "https://www.imdb.com/name/nm0004715/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Michael Cera",
                        "character": "George Michael Bluth",
                        "imdb": "https://www.imdb.com/name/nm0148418/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Portia De Rossi",
                        "character": "Lindsay Bluth",
                        "imdb": "https://www.imdb.com/name/nm0005577/?ref_=nv_sr_1"
                    }
                ]
            },
            "impractical_jokers": {
                "cast": [
                    {
                        "actor": "James Murray",
                        "showName": "Murr",
                        "imdb": "https://www.imdb.com/name/nm2098978/?ref_=nv_sr_2"
                    },
                    {
                        "actor": "Joseph Gatto",
                        "showName": "Joe",
                        "imdb": "https://www.imdb.com/name/nm2665746/?ref_=nv_sr_2"
                    },
                    {
                        "actor": "Sal Vucano",
                        "showName": "Sal",
                        "imdb": "https://www.imdb.com/name/nm1742600/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Brian Quinn",
                        "showName": "Q",
                        "imdb": "https://www.imdb.com/name/nm1978079/?ref_=nv_sr_1"
                    }
                ]
            },
            "family_guy": {
                "cast": [
                    {
                        "actor": "Seth MacFarlane",
                        "character": ["Peter Griffin", "Brian Griffin", "Stewie Griffin"],
                        "imdb": "https://www.imdb.com/name/nm0532235/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Mila Kunis",
                        "character": "Meg Griffin",
                        "imdb": "https://www.imdb.com/name/nm0005109/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Seth Green",
                        "character": "Chris Griffin",
                        "imdb": "https://www.imdb.com/name/nm0001293/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Alex Bornstein",
                        "character": ["Lois Griffin", "Tricia Takanawa", "Barbara Pewterschmidt"],
                        "imdb": "https://www.imdb.com/name/nm0097504/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Patrick Warburton",
                        "character": "Joe Swanson",
                        "imdb": "https://www.imdb.com/name/nm0911320/?ref_=nv_sr_2"
                    }
                ]
            }
        },

        "reality": {
            "keeping_up_with_the_kardashians": {
                "regulars": ["Kim Kardashian", "Khloe Kardashian",
                           "Kylie Jenner", "Kris Kardashian"]
            },
            "american_idol": {
                "judges": ["Simon Cowell", "Randy Jackson", "Paula Abdul"]
            },
            "family_feud": {
                "host": "Steve Harvey"
            },
            "the_bachelorette": {
                "host": "Chris Harrison"
            },
            "duck_dynasty": {
                "cast": ["Phil Robertson", "Willie Robertson",
                       "Si Robertson", "Jase Robertson", "Korie Robertson"]
            }
        },

        "talk": {
            "real_time_with_bill_maher": {
                "host": "Bill Maher"
            },
            "the_view": {
                "host": ["Whoopi Goldberg", "Barbara Walters",
                       "Joy Behar", "Star Jones", "Meredith Vieira"]
            },
            "the_daily_show": {
                "host": "Trevor Noah"
            },
            "conan": {
                "host": "Conan O'Brien"
            },
            "late_night_with_seth_meyers": {
                "host": "Seth Meyers"
            }
        },

        "kids": {
            "Spongebob": {
                "cast": [
                    {
                        "actor": "Tom Kenny",
                        "character": "SpongeBob",
                        "imdb": "https://www.imdb.com/name/nm0444786/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Clancy Brown",
                        "character": "Mr. Krabs",
                        "imdb": "https://www.imdb.com/name/nm0000317/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Bill Fagerbakke",
                        "character": "Patrick Star",
                        "imdb": "https://www.imdb.com/name/nm0265067/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Roger Bumpass",
                        "character": "Squidward Tentacles",
                        "imdb": "https://www.imdb.com/name/nm0120309/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Carolyn Lawrence",
                        "character": "Sandy Cheeks",
                        "imdb": "https://www.imdb.com/name/nm0492657/?ref_=nv_sr_1"
                    }
                ]
            },
            "jimmy_neutron": {
                "cast": [
                    {
                        "actor": "Debbi Derryberry",
                        "character": "Jimmy Neutron",
                        "imdb": "https://www.imdb.com/name/nm0220635/?ref_=nv_sr_3"
                    },
                    {
                        "actor": "Rob Paulsen",
                        "character": "Carl Wheezer",
                        "imdb": "https://www.imdb.com/name/nm0667326/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jeffrey Garcia",
                        "character": "Sheen Estevez",
                        "imdb": "https://www.imdb.com/name/nm0305261/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Andrea Martin",
                        "character": "Ms. Fowl",
                        "imdb": "https://www.imdb.com/name/nm0551908/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Carolyn Lawrence",
                        "character": "Cindy Vortex",
                        "imdb": "https://www.imdb.com/name/nm0492657/?ref_=nv_sr_1"
                    }
                ]
            },
            "yu_gi_oh": {
                "cast": [
                    {
                        "actor": "Dan Green",
                        "character": "Yami Yugi",
                        "imdb": "https://www.imdb.com/name/nm0337751/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Wayne Grayson",
                        "character": "Joey Wheeler",
                        "imdb": "https://www.imdb.com/name/nm0969901/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Ted Lewis",
                        "character": "Bakura",
                        "imdb": "https://www.imdb.com/name/nm0507793/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Eric Stuart",
                        "character": "Seto Kaiba",
                        "imdb": "https://www.imdb.com/name/nm0835688/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Amy Birnbaum",
                        "character": "Téa Gardner",
                        "imdb": "https://www.imdb.com/name/nm0083671/?ref_=nv_sr_1"
                    }
                ]
            },
            "dexters_lab": {
                "cast": [
                    {
                        "actor": "Christine Cavanaugh",
                        "character": "Dexter",
                        "imdb": "https://www.imdb.com/name/nm0004815/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Kath Soucie",
                        "character": "Mom",
                        "imdb": "https://www.imdb.com/name/nm0815718/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Jeff Bennett",
                        "character": "Dad",
                        "imdb": "https://www.imdb.com/name/nm0071818/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Kat Cressida",
                        "character": "Dee Dee",
                        "imdb": "https://www.imdb.com/name/nm0187570/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Tom Kenny",
                        "character": "Val Hallen",
                        "imdb": "https://www.imdb.com/name/nm0444786/?ref_=nv_sr_1"
                    }
                ]
            },
            "teen_titans": {
                "cast": [
                    {
                        "actor": "Scott Menville",
                        "character": "Robin",
                        "imdb": "https://www.imdb.com/name/nm0579914/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Tara Strong",
                        "character": "Raven",
                        "imdb": "https://www.imdb.com/name/nm0152839/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Khary Payton",
                        "character": "Cyborg",
                        "imdb": "https://www.imdb.com/name/nm1146051/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Greg Cipes",
                        "character": "Beast Boy",
                        "imdb": "https://www.imdb.com/name/nm0996651/?ref_=nv_sr_1"
                    },
                    {
                        "actor": "Hynden Walch",
                        "character": "Starfire",
                        "imdb": "https://www.imdb.com/name/nm0906945/?ref_=nv_sr_1"
                    }
                ]
            }
        }

    }
}

We will be happy to hear your thoughts

Leave a reply