Python Nested Dictionary
A nested dictionary in Python is a dictionary that contains other dictionaries as values. This allows you to represent complex, hierarchical data structures.
Creating a nested dictionary
A nested dictionary can be created by placing dictionaries inside other dictionaries.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
},
"ethan": {
"age": 30,
"city": "Toronto",
"occupation": "Pilot"
}
}
Accessing values in a nested dictionary
To access values in a nested dictionary, you use multiple keys in a sequence.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
},
"ethan": {
"age": 30,
"city": "Toronto",
"occupation": "Pilot"
}
}
# Accessing the 'age' value for 'james'
james_age = people["james"]["age"]
print(james_age) # Output: 37
# Accessing the 'city' value for 'ethan'
ethan_city = people["ethan"]["city"]
print(ethan_city) # Output: Toronto
Modifying values in a nested dictionary
To update a value in a nested dictionary, navigate to the appropriate level of keys and assign a new value to it.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
},
"ethan": {
"age": 30,
"city": "Toronto",
"occupation": "Pilot"
}
}
# Updating the "age" value for "james"
people["james"]["age"] = 25
# Updating the "city" value for "james"
people["james"]["city"] = "Seattle"
print(people)
Output:
{'james': {'age': 25, 'city': 'Seattle', 'occupation': 'Teacher'}, 'ethan': {'age': 30, 'city': 'Toronto', 'occupation': 'Pilot'}}
Adding an item to a nested dictionary
To add an item to a nested dictionary, insert a new key-value pair at the appropriate level within the dictionary structure.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
}
}
# Adding new item to the nested dictionary
people["melina"] = {"age": 25, "city": "Houston", "occupation": "actress"}
print(people)
Output:
{'james': {'age': 37, 'city': 'London', 'occupation': 'Teacher'}, 'melina': {'age': 25, 'city': 'Houston', 'occupation': 'actress'}}
Deleting items from a nested dictionary
You can delete items from a nested dictionary using the del
statement or the pop()
method.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
},
"ethan": {
"age": 30,
"city": "Toronto",
"occupation": "Pilot"
}
}
del people["james"]
print(people)
people.pop("ethan")
print(people)
Output:
{'ethan': {'age': 30, 'city': 'Toronto', 'occupation': 'Pilot'}}
{}
Iterating through a nested dictionary
You can use loops to iterate through a nested dictionary.
For example:
people = {
"james": {
"age": 37,
"city": "London",
"occupation": "Teacher"
},
"ethan": {
"age": 30,
"city": "Toronto",
"occupation": "Pilot"
}
}
for person, details in people.items():
print("name:", person)
for key, value in details.items():
print(f"{key}: {value}")
print()
Output:
name: james
age: 37
city: London
occupation: Teacher
name: ethan
age: 30
city: Toronto
occupation: Pilot