enumerate dictionary python\ code example
Example 1: iterate through dictionary
#iterate the dict by keys
for key in a_dict:
print(key)
#iterate the dict by items - (key,value)
for item in a_dict.items():
print(item)
#iterate the dict by values
for value in a_dict.values():
print(value)
Example 2: enumerate dictionary python
# Iterate over dictionary using enumerate()
mydict = {
"one": 1,
"two": 2,
"three": 3
}
# You can replace `index` with an underscore to ignore the index
# value if you don't plan to use it.
for index, item in enumerate(mydict):
print(f"{item}: {mydict.get(item)}")
# OUTPUT:
# one: 1
# two: 2
# three: 3