Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dictionary basics
Dictionaries in loops and conditional statements
9. Iterating over dictionaries – keys()
Dictionaries in functions
Summary

Instruction

That's it! If you want to iterate over dictionary keys, you can use the keys() function. Take a look:

phonebook = {'Mary': '8349374', 'Anne': '7439834', 'John': '3472983'}

for key in phonebook.keys():
  print(key, '=', phonebook[key])

The expression phonebook.keys() returns a list of all keys present in a dictionary. You can use a regular for loop to iterate over the list.

The code above will simply print all keys and values in the dictionary. Again, remember that the order of keys might be different from the order you specified when defining the dictionary, since dictionaries are unordered data structures.

Exercise

For each Windows version in the dictionary, print the following: {version_name} was released in {year}.

Stuck? Here's a hint!

Use:

for key in os_releases.keys():