Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nested Dictionaries
๐ Scenario: You work at a small bookstore. You want to organize information about books by category and details like author and price.
๐ฏ Goal: Create a nested dictionary to store book details by category, then print the price of a specific book.
๐ What You'll Learn
Create a nested dictionary with categories as keys and dictionaries of books as values
Each book dictionary should have keys 'author' and 'price'
Access nested dictionary values using keys
Print the price of a specific book
๐ก Why This Matters
๐ Real World
Nested dictionaries help organize complex data like product catalogs, user profiles, or settings grouped by categories.
๐ผ Career
Understanding nested dictionaries is important for data handling, configuration management, and working with JSON-like data in many programming jobs.
Progress0 / 4 steps
1
Create the nested dictionary
Create a dictionary called bookstore with two categories: 'Fiction' and 'Science'. Each category should be a dictionary with these books and details exactly: 'Fiction': {'The Great Gatsby': {'author': 'F. Scott Fitzgerald', 'price': 10.99}, '1984': {'author': 'George Orwell', 'price': 8.99}} 'Science': {'A Brief History of Time': {'author': 'Stephen Hawking', 'price': 15.99}, 'The Selfish Gene': {'author': 'Richard Dawkins', 'price': 12.99}}
Python
Hint
Use curly braces to create dictionaries. The value for each category is another dictionary with book titles as keys.
2
Set the category to look up
Create a variable called category and set it to the string 'Fiction'.
Python
Hint
Just assign the string 'Fiction' to the variable category.
3
Access the nested dictionary
Create a variable called book and set it to the string '1984'. Then create a variable called price and set it to the price of the book '1984' in the category inside bookstore.
Python
Hint
Use square brackets to access nested dictionaries: bookstore[category][book]['price'].
4
Print the price
Write a print statement to display the price stored in the variable price.
Python
Hint
Use print(price) to show the price.
Practice
(1/5)
1. What is a nested dictionary in Python? Example: {'person': {'name': 'Alice', 'age': 30}}
easy
A. A dictionary with only one key
B. A dictionary inside another dictionary
C. A list inside a dictionary
D. A dictionary with numeric keys only
Solution
Step 1: Understand dictionary structure
A dictionary stores key-value pairs. Nested means one value is itself a dictionary.
Step 2: Analyze the example
In {'person': {'name': 'Alice', 'age': 30}}, the value for 'person' is another dictionary.
Final Answer:
A dictionary inside another dictionary -> Option B