In this Python Tutorial, you will learn how to iterate over keys in a dictionary using dict.keys() method.

Python – Iterate over Dictionary Keys

To iterate over Dictionary Keys in Python, get the dictionary keys using dict.keys() method and use a for loop on this keys object to traverse through each key in the dictionary.

The following code snippet demonstrates how to iterate over keys in a Python Dictionary.

</>
Copy
for key in dictionary.keys():
    #statement(s)

Example

1. Iterate over dictionary keys

In the following example, we have taken a dictionary with three key:value pairs. We will get the keys from this dictionary using dict.keys() method and use For Loop to traverse through each key.

Python Program

</>
Copy
dictionary = {'a': 58, 'b': 61, 'c': 39}

for key in dictionary.keys():
    print(key)

Output

a
b
c

Reference tutorials for the above program

Conclusion

In this Python Tutorial, we learned how to iterate over dictionary keys in Python with example.