How to Iterate over Dictionary Keys in Python?

[Pages:2]How to Iterate over Dictionary Keys in Python?

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.

for key in dictionary.keys(): #statement(s)

Example ? 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 Python For Loop to traverse through each key. Python Program

dictionary = {'a': 58, 'b': 61, 'c': 39} for key in dictionary.keys():

print(key)

Output

a b c

Conclusion

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

Python Programming

Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments

Control Statements

Python If Python If Else Python While Loop Python For Loop

Python String

Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings

Functions

Python Functions

Python Collections

Python List Python Dictionary

Advanced

Python Multithreading

Useful Resources

Python Interview Questions

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download