In this post, we are going to learn How to Enumerate dictionary in Python. The enumerate() method is used to iterate through the sequence by using an index.
Enumerate() in Python
The Enumerate() method add a counter/index to an sequence(tuple,list,dictionary) and return as an enumerate object.The enumerate object can be directly used with for loop to iterate over and easily converted to list or tuple by using list() or tuple() method.
Syntax
Enumerate(iterable,start)
Parameters
- iterable: an iterable(list,tuple,dictionary) that we want to iterate by index
- start : It is an optional parameter is the counter from which index will start.if not provided default value will be 0.
1. Simple Enumerate a dictionary
In this example, we simply enumerate a dictionary by passing dictionary name to enumerate() method.
Program Example
dict_average = {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80,
"Dhoni" : 90
}
for index, key in enumerate(dict_average):
print('Index:',index ,',key:',key, ',value : ', dict_average[key])
Output
Index: 0 ,key: Sachin ,value : 56
Index: 1 ,key: Dravid ,value : 70
Index: 2 ,key: Sourav ,value : 80
Index: 3 ,key: Dhoni ,value : 90
1. Enumerate all key of dictionary
The dictionary. keys() function returns all dictionary keys as a list. The enumerate() function adds an index/counter to all keys of the dictionary and returns an enumerated object converted to a list by using the list() method. It is a list of tuples that contain the keys and their corresponding index/counter.
Later we are using for loop to iterate over enumerate objects.
Python Program to enumerate all key of dictionary
dict_average = {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80,
"Dhoni" : 90
}
keys_with_index = list(enumerate(dict_average.keys()))
print(keys_with_index)
for index, key in enumerate(dict_average.keys()):
print('Index:',index , ' : ', key)
Output
[(0, 'Sachin'), (1, 'Dravid'), (2, 'Sourav'), (3, 'Dhoni')]
Index: 0 : Sachin
Index: 1 : Dravid
Index: 2 : Sourav
Index: 3 : Dhoni
3. Enumerate all value of dictionary
The dictionary. values() function returns all dictionary values as a list. The enumerate() function adds an index/counter to the sequence of all values of the dictionary returns an enumerate object. Later we are using for loop to iterate over enumerate objects.
Python Program
dict_average = {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80,
"Dhoni" : 90
}
for index, value in enumerate(dict_average.values()):
print('Index:',index , ' : ', value)
Output
Index: 0 : 56
Index: 1 : 70
Index: 2 : 80
Index: 3 : 90
4.Enumerate all key-value of dictionary
The dictionary. items() function is used to get all keys-values of the dictionary. The enumerate() function adds an index/counter to the sequence of all values of the dictionary returns an enumerate object. Later we are using for loop to iterate over enumerate objects.
Python Program
dict_average = {
"Sachin": 56,
"Dravid" : 70 ,
"Sourav" : 80,
"Dhoni" : 90
}
for index,(key, value) in enumerate(dict_average.items()):
print('Index:',index ,'key:',key, ' : ', value)
Output
Index: 0 key: Sachin : 56
Index: 1 key: Dravid : 70
Index: 2 key: Sourav : 80
Index: 3 key: Dhoni : 90
Summary
In this post, we have learned how to Enumerate dictionary in Python by using enumerate() method