Iterating Over a Dictionary in Python

A dictionary in Python is very similar to a dictionary in the real world. You have a key and a definition. It is accessed by a key and not a definition.

There are few ways you can loop through dictionaries in Python 3.

Example dictionary:

Iterate over keys

output

Iterate over values

output

Iterate over key/value pairs

output

Iterate over keys in sorted order

output

Iterate over nested dictionary

You can also iterate through a nested dictionary.

Nested dictionary example:

code

output