Loading lesson...
Navigate nested data with confidence
Navigate nested data with confidence
Topics covered: Iterating Over Dictionaries, Dictionary View Objects, Merging Dictionaries, Nested Dictionaries, Dictionaries with Lists, Copying Dictionaries, Dictionary Comprehensions, Common Dictionary Patterns
Iterating means looping through each item in a collection. With lists, you iterate over elements by position. With dictionaries, you have three choices: iterate over keys, values, or both. Let's explore each approach. Iterating Over Keys When you loop directly over a dictionary, you iterate over its keys. This is the default behavior and the most common pattern: Iterating Over Values Iterating Keys and Values Fill in the blanks to complete two tasks: sum up all the prices, then get a list of all
Converting Views to Lists The .update() Method The .setdefault() Method
Combining two dictionaries into one is a common operation. Python provides several ways to do this, each with different trade-offs. Method 1: Using .update() Method 2: | Operator (3.9+) Modern Python (3.9 and later) introduced the | operator for merging dictionaries. This creates a NEW dictionary without modifying the originals: When both dictionaries have the same key, the value from the right-side dictionary (after the |) wins. This is intuitive: you're "applying" user preferences on top of de
Dictionary values can be any Python type, including other dictionaries. This allows you to represent complex, hierarchical data. Real-world data is almost always nested. Each bracket access goes one level deeper. The expression company["address"] returns the nested dictionary, and then ["city"] accesses a key within that nested dictionary. Modifying Nested Values You can modify nested values using the same chained bracket syntax: Safe Nested Access Following a few simple rules prevents most nest
A very common pattern is using lists as dictionary values. This lets you group multiple items under a single key, such as all orders for a customer or all tags for an article: This pattern appears constantly in data engineering. API responses, configuration files, database records with multi-valued fields all use dictionaries with lists. Building Lists Over Time
When you assign a dictionary to a new variable, you don't create a copy. Both variables point to the same dictionary. This is critical to understand: Shallow Copy with .copy() Fill in the blanks to take a snapshot of a dictionary, then modify the original. Think about what each operation returns. Understanding reference semantics vs copy semantics is crucial for avoiding bugs when passing dictionaries to functions. Functions receive a reference by default, so any mutation inside the function is
Just like list comprehensions create lists in a single expression, dictionary comprehensions create dictionaries. The syntax uses curly braces with a key:value expression: Filter with Comprehensions You can add an if clause to filter which items are included: Transforming Keys or Values Comprehensions are powerful for transforming dictionary data: Try building a dictionary comprehension yourself. Choose the right expression to transform a list of names into a dictionary of name-length pairs. Dic
These patterns appear constantly in real code. Once you recognize them, you will reach for them instinctively when solving similar problems. Inverting a Dictionary Swapping keys and values is useful when you need reverse lookups: Counting with Dictionaries We saw a counting example in the beginner lesson. Here's a cleaner version using .get(): This counting loop has a bug. The code tries to count letters but crashes on the first iteration. Remove the tile causing the error to fix it. Mastering d