Sometimes you’ll want to store multiple dictionaries in a list, or a list of items as a value in a dictionary. This is called nesting.
You can nest dictionaries inside a list, a list of items inside a dictionary, or even a dictionary inside another dictionary
Think of nesting as a backpack inside another backpack. The outer backpack is your dictionary (e.g., the Avengers roster), and the inner backpack is another dictionary or list (e.g., Iron Man’s suit stats or a list of his repulsor blasts).
Imagine you're building your own streaming service — like Netflix, but cooler. You don’t just want a single movie... you want a whole library. Each movie is like a dictionary — it stores all the important details:
🎞️ Title
🎭 Genre
📆 Release Year
🔞 MPAA Rating
But what good is one movie? You want a collection! That’s where the list comes in — it acts like your movie binder, holding all your dictionaries in one place. This way, you can:
🎥 Flip through every movie in your library
➕ Add new movies on the fly
🔍 Search, display, and count how many epic titles you’ve got
🧠 Keep your data organized and expandable
Instructions:
Imagine you’re curating a collection of movies for a streaming service. Each movie is a dictionary with details (title, genre, rating), and the collection is a list of these dictionaries. This structure lets you store multiple movies in one place. Your movie must include 4 fields: TITLE, GENRE, RELEASE_YEAR, and MPAA_RATING,
Sample output is shown
Make Sure you error check for KeyError
Once that's done:
Create at least 4 movies (hardcoded, or ask for user input)
Allow a user to ADD a movie (so they are prompted)
Add it to the binder
Display All Movies in the Movie Database
Print How Many Movies are in the database (len)
Sample Output Window
Remember, you need to:
Create Movies
Offer Option to Add Movie
Add them to your database
Loop through each movie and print its information nicely formatted:
Note: This should be good review, as we've covered this in the past. Give yourself at most 20 minutes to complete this task!!
In your movie database, sometimes one piece of information just isn’t enough.
Take the genre for example — is The LEGO Batman Movie just a comedy? Nope. It’s Action, Comedy, Animation, and Superhero all rolled into one!
When you store a list inside a dictionary, you can pack multiple values under a single category. It’s like giving each movie its own playlist of genres, cast list, or even languages available.
Instead of this...
We see this...
Now your database can:
🎭 Handle multiple genres per movie
👥 Track full cast lists
🌍 List all subtitles or languages available
🎬 Keep related movie titles or sequels in one place
Using lists inside dictionaries makes each movie entry flexible and powerful — just like a real streaming service!
Here we see 2 examples of how to use this. We can either print out the list as a LIST, or we can use our for loop, and print each one individually!!
Now that you have a movie collection, let’s enhance it by adding a list of actors for each movie (introducing lists within dictionaries) and performing more tasks to analyze the database. This sets the stage for introducing dictionaries within dictionaries in future activities (e.g., production details).
Build off your movie collection from Activity 1. Add a 'cast' key to each movie, where the value is a list of at least 3 actors (strings). Then, perform the following tasks:
Add the 'cast' list to existing movies (hardcoded for simplicity).
Allow the user to add a cast list for the new movie they added in Activity 1.
Display all movies with their cast lists.
Search for movies by genre (user input).
Search for movies featuring a specific actor (user input).
BONUS: Sort and display movies by release year (ascending order).
Hint: You'll be using for / in to loop through and if/in to see if it exists!!
Sample Output:
When you're building a database, sometimes one dictionary just isn't enough. If you're keeping track of multiple movies, and each movie has multiple pieces of information, you need a way to keep everything grouped, searchable, and organized. This is where a dictionary inside a dictionary comes in.
We will create a dictionary within a dictionary by defining users, and inside that, we will add information for each user (in the form of a dictionary!!)
iron_man = {
"Title": "Iron Man",
"Release Year": 2008,
"Genre": ["Sci-Fi", "Action", "Adventure"],
"Cast": ["RDJ", "Gwyneth Paltrow"],
"Ratings": {
"MPAA Rating": "PG-13",
"Rotten Tomatoes Tomatometer": 94,
"Rotten Tomatoes Audience Score": 91,
"IMDB Rating": 7.9
}
}
For this, where there's a dictionary inside another dictionary, we can use DIRECT ACCESS to get to the NESTED VALUES!!
Example: Print the different Ratings:
iron_man["Ratings"]["MPAA Rating"]
This prints PG-13
iron_man["Ratings"]["IMDB Rating"]
This prints 7.9
See below how we can access the entire dictionary
movies = {
"Inception": {
"rating": "PG-13",
"year": 2010,
"genre": "Sci-Fi"
},
"The Matrix": {
"rating": "R",
"year": 1999,
"genre": "Action"
}
}
You’re creating a collection of movies — and each movie is a dictionary of info. You store all the movie dictionaries inside a list.
Good for going through every movie one by one with a for loop.
Each movie is an anonymous entry in the list (you don't have a specific key for it).
You can’t jump directly to “Inception” — you have to loop through the list to find it.
You’re creating a collection of movies — and each movie is a dictionary of info. You store all the movie dictionaries inside a list.
Great for looking up a specific movie quickly by title
e.g., movies["Inception"]
Acts like a labeled folder system, where each folder (key) stores movie info.
Easier for direct access, harder to loop through in order unless you sort or extract the keys.
Use a list of dictionaries when:
You don’t care about looking up items directly by name.
You just want to loop and display everything.
Use a dictionary of dictionaries when:
You want fast access to a specific entry (like a movie title).
You want your data to be organized by unique keys.
Welcome to Streamify+UltraMax — the world’s third-most-popular streaming service (right after FlixNet and GoatTV). You’ve just been promoted to Lead Movie Curator and Python Wizard, responsible for maintaining and managing a growing movie collection. Each movie in your database must now include not just the basics like title, genre, and release_year, but also:
A full cast list (list of actors)
A shiny new ratings dictionary featuring:
🎬 'mpaa_rating': like “PG-13” or “R”
⭐ 'imdb_rating': a float (e.g., 8.2)
🍅 'rotten_tomatoes_tomatometer': critics score (0–100)
🍿 'rotten_tomatoes_audience_score': audience score (0–100)
KeyError:
If a user tries to access a nonexistent key.
ValueError:
If the user enters a wrong type (e.g., letters instead of numbers).
Invalid menu options or empty input.
Using your previous movie collection from Activity 2, remove the production field and add the new ratings dictionary to each movie. Then, create an interactive menu system that loops until the user exits.
🎬 Menu Options (Minimum 5):
Display All Movies
Show full details for each movie, including cast list and ratings.
Add a New Movie
Prompt the user for all fields: title, genre, release year, cast (list), and ratings.
Search by MPAA Rating
Ask for a rating (like “PG”) and list all matching movies.
Update IMDb Rating
Let the user select a movie and enter a new IMDb score.
Search by Actor
Enter an actor’s name and see all the movies they appear in.
🔒 Bonus 1: Rotten Showdown
Display the top 3 highest-rated movies by Rotten Tomatoes audience score.
Use sorting! Winner gets the 🍅 Crown.
🎞️ Bonus 2: Actor Tracker Pro
Create an actor database on the side: show how many movies each actor is in.
🧾 Bonus 3: Save the Collection
Write the current movie_collection to a .json file so it can be reused later.
🎲 Bonus 4: Random Recommendation
Create a “Pick a Movie for Me!” option that selects a random movie from the database and displays its info.
Store your movies in a list called movie_collection
Use functions to organize your code
Use input() carefully and validate what the user types
Practice accessing values in nested dictionaries and lists
Don’t forget: movie["ratings"]["imdb_rating"] is how you go inception-level deep into your data
This slick example is how we can leverage isinstance
We saw this way back in unit 2 and 3 so we could see what type an object was!
If isinstance finds a list, we can loop through all the items in it
If isinstance finds a dictionary, we look for the sub-values by returning items