Nested data

Real data is usually nested: a list of dicts from a JSON API, or a dict of lists after grouping. You reach inside one step at a time, left to right: orders[0]["items"][1] is the second item of the first order.

To walk nested data, write one loop per level and name each level clearly: for order in orders: then for item in order["items"]:. Good names matter more here than anywhere else.

setdefault(key, []) returns the list stored at key, creating it first if needed, so you can call .append or .extend on the result directly. It does the same job as defaultdict(list) with a plain dict.

Interview questions often hand you a list of records and ask for a summary: totals per customer, the latest event per user, and so on. Turn that into a dict keyed by what you group on.

Run it, then add a function that counts orders per customer.