Python: Iterate over dictionaries using for loops

Python dictionary: Exercise-9 with Solution

Write a Python program to iterate over dictionaries using for loops.

Sample Solution:-

Python Code:

d = {'Red': 1, 'Green': 2, 'Blue': 3} 
for color_key, value in d.items():
     print(color_key, 'corresponds to ', d[color_key]) 

Sample Output:

Red corresponds to  1
Green corresponds to  2
Blue corresponds to  3

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python script to merge two Python dictionaries.
Next: Write a Python program to sum all the items in a dictionary.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()