How to iterate over dictionary in Python
Answered
36
What is the best way to loop through a dictionary keys and values?
S
Asked by
security_pro
Bronze
•
259 rep
1 Answer
15
# Iterate over keys
for key in my_dict:
print(key)
# Iterate over values
for value in my_dict.values():
print(value)
# Iterate over key-value pairs
for key, value in my_dict.items():
print(f"{key}: {value}")
# With enumerate
for i, (key, value) in enumerate(my_dict.items()):
print(f"{i}: {key} = {value}")
A
Platinum
•
151 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer