How to iterate over a dictionary in python?

Iterating over dictionaries can be done by keys, values, or both.

Example:


  for key in my_dict:
      print(key, my_dict[key])
  

Solution:

You can also use the items() method to iterate over both keys and values simultaneously:


  for key, value in my_dict.items():
      print(key, value)
  

Beginner's Guide to Python