Practice · Python

Practice Nested Data Structures

Model real data with lists of dicts and dicts of lists, and read/update deeply nested values.

Nested Data Structureswarming python…

Total Harvest Weight

You are given a garden represented as a list of plots. Each plot is a dictionary with a key "plants" whose value is a list of plant dictionaries. A plant dictionary has keys "type" (a string) and "weight" (an integer or float) indicating how many kilograms that plant contributes to the harvest.

Write a function total_harvest_weight(garden) that returns the sum of the weights of all plants in the whole garden. If the garden is empty or a plot contains no plants, the function should return 0.

Example:

garden = [
    {"plants": [{"type": "tomato", "weight": 1.2}, {"type": "cucumber", "weight": 0.8}]},
    {"plants": [{"type": "carrot", "weight": 0.5}]},
]
print(total_harvest_weight(garden))  # 2.5
Press Run to see output, or Check to grade against the tests.

Want a whole course built around nested data structures?

Start my path — free

Adaptive practice, streaks, projects with real code review. Free tier gates AI help, never content.