Example for key value in dict Python

For loop will iterate through a dictionary to get key-value in Python. In dictionary iterates over all the keys and helps us to access the key-value pair one after the other in the loop

Iterate Through a Dictionary in Python

Simple example code gets the key value of dict using for loop in Python. In the loop, you will get keys and use those keys in the get method to get value for this key.

dict1 = {1: 'A', 2: 'B', 3: 'C', 4: 'D'}

for key in dict1:
    print(key, dict1.get(key))

Output:

Example for key value in dict Python

Do comment if you have any doubts and suggestions on this Python dict topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.