Iterate through dictionary in python

In this post, we will see how to iterate through dictionary in python.
You can use for key in dict.keys(): to iterate over keys of dictionary.

You can use for value in dict.values(): to iterate over values of dictionary.

You can use items() method to iterate over key-value pairs of dictionary.

Let’s understand with the help of example.

Output:

Dictionary: {1: ‘one’, 2: ‘two’, 3: ‘three’}
================
Printing keys:
================
1
2
3
================
Printing values:
================
one
two
three
============================
Printing key-value pairs:
============================
1 : one
2 : two
3 : three

That’s all about iterating over dictionary in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *