0
0
Pythonprogramming~10 mins

Nested dictionaries in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested dictionaries
Start: Create outer dictionary
Add key with inner dictionary
Access outer key
Access inner dictionary
Access inner key's value
Use or modify value
End
We create a dictionary that holds another dictionary as a value, then access keys step-by-step inside both dictionaries.
Execution Sample
Python
person = {
    "name": "Alice",
    "contact": {"email": "alice@example.com", "phone": "123-456"}
}
print(person["contact"]["email"])
This code creates a nested dictionary for a person and prints the email from the inner dictionary.
Execution Table
StepActionDictionary StateAccess/Output
1Create outer dictionary with keys 'name' and 'contact'{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}N/A
2Access outer key 'contact'{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}{"email": "alice@example.com", "phone": "123-456"}
3Access inner key 'email' inside 'contact'{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}"alice@example.com"
4Print the accessed email value{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}alice@example.com
💡 Finished accessing nested dictionary and printed the email value.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
personundefined{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}
Key Moments - 2 Insights
Why do we use two sets of square brackets to access the email?
The first bracket accesses the outer dictionary's 'contact' key which holds another dictionary. The second bracket accesses the 'email' key inside that inner dictionary, as shown in execution_table steps 2 and 3.
Can we access 'email' directly from the outer dictionary?
No, because 'email' is inside the nested dictionary under 'contact'. You must first access 'contact' then 'email', as shown in the step-by-step access in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of person after Step 1?
A"alice@example.com"
B{"name": "Alice", "contact": {"email": "alice@example.com", "phone": "123-456"}}
C{"email": "alice@example.com", "phone": "123-456"}
Dundefined
💡 Hint
Check the 'Dictionary State' column at Step 1 in the execution_table.
At which step do we get the inner dictionary as the access result?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Access/Output' column in the execution_table to find when the inner dictionary is accessed.
If we tried to access person["email"] directly, what would happen?
AIt would return "alice@example.com"
BIt would return None
CIt would cause a KeyError
DIt would return the whole nested dictionary
💡 Hint
Recall that 'email' is not a key in the outer dictionary, only inside the nested one, so direct access fails.
Concept Snapshot
Nested dictionaries store dictionaries inside dictionaries.
Access with multiple brackets: outer[key1][key2].
First get inner dictionary, then inner value.
Useful for structured data like contacts or settings.
Always check keys exist to avoid errors.
Full Transcript
This lesson shows how nested dictionaries work in Python. We start by creating an outer dictionary named 'person' with keys 'name' and 'contact'. The 'contact' key holds another dictionary with 'email' and 'phone'. We access the nested 'email' by first getting 'contact' from 'person', then 'email' from that inner dictionary. The execution table traces each step, showing the dictionary state and what is accessed or printed. Key moments clarify why two brackets are needed and why direct access to inner keys from the outer dictionary fails. The quiz tests understanding of dictionary states and access steps. The snapshot summarizes the concept for quick review.