Loading section...

Iterating Over Dictionaries

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 item names. Since Python 3.7, dictionary order is guaranteed to be insertion order. This means looping over keys, values, or items always produces them in the order they were added, making dictionary iteration predictable.