Nested Dictionaries
Part of the Logic & Flow section of Coddy's Python journey — lesson 13 of 78.
A nested dictionary is a dictionary within a dictionary. It allows you to organize complex data structures, making it easier to work with related information.
Example:
students = {
"Alice": {"age": 20, "grade": "A"},
"Bob": {"age": 22, "grade": "B"}
}
# Accessing a nested value
print(students["Alice"]["age"]) # Output: 20
# Adding a new key-value pair to a nested dictionary
students["Alice"]["major"] = "Math"
print(students["Alice"]) # Output: {'age': 20, 'grade': 'A', 'major': 'Math'}Nested dictionaries are useful when you need to group related information together, such as storing details about students, employees, or products.
Cheat sheet
A nested dictionary is a dictionary within a dictionary, useful for organizing complex data structures:
students = {
"Alice": {"age": 20, "grade": "A"},
"Bob": {"age": 22, "grade": "B"}
}
# Accessing nested values
print(students["Alice"]["age"]) # Output: 20
# Adding to nested dictionary
students["Alice"]["major"] = "Math"Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter