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 Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Invokes the provided function after ms milliseconds:


from time import sleep

def tips_delay(fn, ms, *args):
  sleep(ms / 1000)
  return fn(*args)
  lambda x: print(x),