- How to Enumerate dictionary in Python
- Enumerate() in Python
- Syntax
- Parameters
- 1. Simple Enumerate a dictionary
- Python Program to enumerate all key of dictionary
- 3. Enumerate all value of dictionary
- Python Program
- 4.Enumerate all key-value of dictionary
- Summary
- Enumerate Dictionary in Python
- Related Article — Python Dictionary
How to Enumerate dictionary in Python
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
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.
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)
[(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)
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.
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)
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
Enumerate Dictionary in Python
The enumerate() function in Python returns an enumerate-type object and adds a counter variable to iterate over a list or some other type of collection. It makes looping over such objects easier.
We can view the contents of an enumerate object when we pass it to the list() function. For example:
l = ['a','b','c'] print(list(enumerate(l)))
We can also use the enumerate() function with dictionaries as well.
The following example shows an elementary example.
d1 = 'a' : 15,'b' : 18,'c' : 20> for i,j in enumerate(d1): print(i,j)
Notice that we directly passed the dictionary to the enumerate() function, and it only assigned the counter variable to the keys of the dictionary and not to the values. So when we iterate over this object, we could only access the counter variable and the dictionary’s keys.
To enumerate both keys and values, we can use the dictionary items() method. The items() method returns an object with the key-value pairs as tuples. The following example shows how we can use the items() method with the enumerate() function and access both the key and its corresponding value.
d1 = 'a' : 15,'b' : 18,'c' : 20> for i, (j,k) in enumerate(d1.items()): print(i,j,k)
If we only want the dictionary elements without their keys, we can use the values() function. It returns a list containing the dictionary values.
The following code shows how:
d1 = 'a' : 15,'b' : 18,'c' : 20> for i,j in enumerate(d1.values()): print(i,j)
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.