Exercise 7: One Level Too Deep — Possible Solution ==================================================================== order = { "id": 5012, "customer": "Dana", "items": ["pen", "ink"], "delivery": {"city": "Leith", "notes": ["ring bell", "fragile"], "fee": 3.5}, } for key, value in order.items(): if isinstance(value, list): print(f"{key}: {', '.join(map(str, value))}") # copy 1 elif isinstance(value, dict): print(f"{key}:") for inner_key, inner_value in value.items(): if isinstance(inner_value, list): print(f" {inner_key}: {', '.join(map(str, inner_value))}") # copy 2 else: print(f" {inner_key}: {inner_value}") else: print(f"{key}: {value}") # Two copies of the list handling. A third level of nesting would need a third. Output: id: 5012 customer: Dana items: pen, ink delivery: city: Leith notes: ring bell, fragile fee: 3.5 WHY THIS WORKS AS AN ANSWER ------------------------------ The list branch had to be written twice: once at the top level and once inside the inner loop, because the inner loop only knows what you tell it. A dictionary nested one level deeper would need the chain written a third time inside that, and so on, and you would have to know the maximum depth in advance. That is the real limit of the inline approach and the strongest argument for the function in the lesson: a function that calls itself for each inner dictionary copes with any depth from a single copy of the code. Use this exercise as the practical example for the function discussion.