← Back to Chapters

Python Looping Through Dictionaries

? Python Looping Through Dictionaries

⚡ Quick Overview

In Python, a dict (dictionary) stores data as key-value pairs. Looping through dictionaries lets you read or process:

  • Only the keys
  • Only the values
  • Both keys and values together

? Key Concepts

  • for key in dict → iterates over keys (default behavior).
  • dict.keys() → explicit view of all keys.
  • dict.values() → view of all values.
  • dict.items() → view of (key, value) pairs.
  • Dictionary views are dynamic: they reflect changes in the dictionary.

? Syntax and Theory

? Looping Through Keys

When you loop directly over a dictionary, Python gives you each key. You can then use the key to access its value.

for key in my_dict: is equivalent to for key in my_dict.keys():

? Looping Through Values

Use values() when you only care about the data, not the key names.

? Looping Through Key-Value Pairs

Use items() to get both key and value at the same time. Each iteration returns a tuple (key, value) which you can unpack.

? Code Examples

? Example: Loop Through Keys

By default, looping through a dictionary iterates over its keys.

? View Code Example
person = {"name": "Alice", "age": 25, "city": "New York"}

for key in person:
    print(key, "->", person[key])
# Output:
# name -> Alice
# age -> 25
# city -> New York

? Example: Loop Through Values

Use values() to loop through only the values in the dictionary.

? View Code Example
for value in person.values():
    print(value)
# Output:
# Alice
# 25
# New York

? Example: Loop Through Key-Value Pairs

Use items() to loop through both keys and values at the same time.

? View Code Example
for key, value in person.items():
    print(f"{key} -> {value}")
# Output:
# name -> Alice
# age -> 25
# city -> New York

? Live Output / Explanation

For the keys loop, each iteration gives a key like "name", which is then used as person[key] to access its value (e.g., "Alice").

For the values loop, person.values() returns all stored values, so the loop directly prints "Alice", 25, and "New York".

For the items loop, person.items() returns pairs like ("name", "Alice"). In each iteration, key receives the first element and value receives the second element, which we format as key -> value.

? Use Cases / When to Use

  • Loop through keys when you need to check or transform key names.
  • Loop through values when you just want to process the data (e.g., sums, averages).
  • Loop through items when you need both key and value (e.g., printing, exporting data).

✅ Tips & Best Practices

  • Prefer items() when you need both keys and values simultaneously.
  • Use values() if you do not need the keys at all.
  • Remember: dictionaries preserve insertion order starting from Python 3.7.
  • Avoid heavily modifying the dictionary structure (adding/removing keys) while looping over it.
  • If you must modify keys, iterate over a list(dict.keys()) copy instead of the original view.
  • Keep your loop body simple and readable—complex logic can be moved into helper functions.

? Try It Yourself

  • Create your own person dictionary and:
    • Loop through only the keys and print each key and its value.
    • Loop through only the values and print them.
    • Loop through items and format output as key -> value.
  • Try adding a new key inside the loop and observe what happens.
  • Write a loop using items() to print: "Alice lives in New York" using the dictionary data.
  • Practice with another dictionary, e.g., product prices, and calculate the total cost by looping over values.