Intermediate · 12 min
Nested data
Read and update lists and dictionaries stored inside one another.
Follow the path
Access nested data one level at a time. First choose the dictionary key, then the list position.
course = {"students": ["Ada", "Mina"]}
print(course["students"][0])Output
Ada
Update a nested value
A nested lookup can appear on the left of an assignment. Only the selected value changes.
profile = {"stats": {"points": 8}}
profile["stats"]["points"] += 2
print(profile["stats"]["points"])Output
10
Nested data
- Read both nested data examples before answering.
- Run the task, compare its exact output, and revise the code if needed.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Total an order
Calculate total from the price and quantity inside order, store it in order under total, and display it.
Follow order to item, then read price and quantity.