How to iterate over dictionary in Python

Answered
Jan 05, 2026 1676 views 1 answers
36

What is the best way to loop through a dictionary keys and values?

S
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
Answered by ahmed_tech 1 week, 2 days ago
Platinum 151 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer

Related Questions

Hot Questions

No hot questions available.