PyGuide

Learn Python with practical tutorials and code examples

How to Iterate Over Dictionary in Python: Complete Guide

Learning how to iterate over dictionary in Python is essential for working with key-value data structures. Python provides several methods to iterate through dictionaries, each serving different purposes. This guide covers all the techniques you need to effectively loop through dictionaries.

What is Dictionary Iteration? #

Dictionary iteration means accessing each element (key-value pair) in a dictionary sequentially. Unlike lists, dictionaries store data as key-value pairs, giving you multiple ways to access the data during iteration.

Basic Dictionary Setup #

Let's start with a sample dictionary that we'll use throughout this guide:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Method 1: Iterate Over Dictionary Keys #

The most basic way to iterate over dictionary in Python is to loop through the keys:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Key Points:

  • Iterating directly over a dictionary gives you the keys
  • Use dict.keys() to be explicit about iterating over keys
  • Access values using dictionary[key] syntax

Method 2: Iterate Over Dictionary Values #

To iterate over only the values in a dictionary:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Use Cases:

  • When you only need the values
  • Calculating statistics (sum, average, max, min)
  • Checking for specific values

Method 3: Iterate Over Dictionary Items (Key-Value Pairs) #

The most common and pythonic way to iterate over dictionary in Python:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Benefits:

  • Most efficient for accessing both keys and values
  • Clean, readable code
  • Pythonic approach

Method 4: Iterate with Index (Enumerate) #

Sometimes you need both the position and the key-value pair:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Advanced Iteration Techniques #

Conditional Iteration #

Filter dictionary items during iteration:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Sorted Iteration #

Iterate in sorted order:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Dictionary Comprehension During Iteration #

Create new dictionaries while iterating:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Nested Dictionary Iteration #

When working with nested dictionaries:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Performance Considerations #

Dictionary Iteration Performance #

Different methods have different performance characteristics:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Performance Tips:

  • Use items() when you need both key and value
  • Use keys() only when you need just the keys
  • Use values() only when you need just the values
  • Avoid repeated dictionary lookups in loops

Common Patterns and Best Practices #

Pattern 1: Filtering and Transforming #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Pattern 2: Aggregation #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Error Handling During Iteration #

Handle potential errors when iterating over dictionaries:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Common Mistakes to Avoid #

1. Modifying Dictionary During Iteration #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

2. Assuming Dictionary Order #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Summary #

When you need to iterate over dictionary in Python, remember these key techniques:

  1. for key in dict: - Iterate over keys only
  2. for value in dict.values(): - Iterate over values only
  3. for key, value in dict.items(): - Iterate over key-value pairs (recommended)
  4. enumerate(dict.items()) - Iterate with index
  5. Conditional iteration - Filter during iteration
  6. Sorted iteration - Process in sorted order

Best Practices:

  • Use items() when you need both keys and values
  • Use get() method for safe key access
  • Don't modify dictionary during iteration
  • Consider performance for large dictionaries
  • Handle missing keys gracefully

Next Steps: