Friday, April 1, 2016

Quick Reference: How to iterate over keys and values at once on a Python 3 dictionary

On Python 3, we use the items() method to iterate over a dictionary's keys and values simultaneously.
dictionary = {"name": "John", "age": "32"}

for key, val in dictionary.items():
    print(key, val)
When the above code is executed, it produces the following result:
age 32
name John

No comments:

Post a Comment