What is Python Enumerate() Function?
In python, we use enumerate() function to keep the count or track of iterations while using iterators over a tuple or list.
Python enumerate() function create a counter in the iterations and return the iteration count in the form of object. In short, the enumerate function returns the items index number and it’s value.
Parameters
- iterable - Objects that supports iteration(list, tuple etc).
- start (optional) - Value from which index will start, By default it is set to ‘0’, we can reset to any value.
Here in this article, we have explained how to use enumerate() function with list, loop and string with example.
Enumerate() List
# Python program to Enumerate() with List
list1 = ["Red","Blue","Green","Orange"]
# creating enumerate
obj1 = enumerate(list1)
print("Return type:",type(obj1))
print(list(enumerate(list1)))
Output
Return type: <class 'enumerate'>
[(0, 'Red'), (1, 'Blue'), (2, 'Green'), (3, 'Orange')]
Explanation
In the above example, we defined a initialized a list ‘MyList’. Then we passed ‘MyList’ as a parameter for enumerate() function. This function returns the enumerate object that we stored in the variable ‘obj1’. The type() methods returns the class type of the object. That is ‘enumerate’ in this case. Then we printed this obj in the last line of the code by converting it into a list.
Enumerate() change start index to 10
# Python program to Enumerate() with List
list1 = ["Red","Blue","Green","Orange"]
print(list(enumerate(list1, 10)))
Output:
[(10, 'Red'), (11, 'Blue'), (12, 'Green'), (13, 'Orange')]
Explanation
In the above example, we set the start value of enumerate() function to 10. Which by default is 0. So later when we printed the enumerate object the indexing starts from 10 rather than starting from 0.
Python Enumerate for loop
# Python program to Enumerate() with List
list1 = ["Red","Blue","Green","Orange"]
# printing the list in object directly
for index,value in enumerate(list1):
print("index: ", index, "Value: ",value)
# changing start index to 100
for index,value in enumerate(list1,100):
print("index: ", index, "Value: ",value)
Output
index: 0 Value: Red
index: 1 Value: Blue
index: 2 Value: Green
index: 3 Value: Orange
index: 100 Value: Red
index: 101 Value: Blue
index: 102 Value: Green
index: 103 Value: Orange
Explanation
In the above example, we have initialized two local variable index and value. These two variables iterates through the enumerate object in the for loop. The index variable holds the indexing number. And the value variable holds the values at those indexes.
The indexing in the first for loop starts from 0. Whereas it starts from 100 in the second.
Enumerate String Python
# Python program to Enumerate() with String
string1 = "Stechies"
# printing the string in object directly
for index,value in enumerate(string1):
print("index: ", index, "Value: ",value)
# changing index to 100
for index,value in enumerate(string1,100):
print("index: ", index, "Value: ",value)
Output
index: 0 Value: S
index: 1 Value: t
index: 2 Value: e
index: 3 Value: c
index: 4 Value: h
index: 5 Value: i
index: 6 Value: e
index: 7 Value: s
index: 100 Value: S
index: 101 Value: t
index: 102 Value: e
index: 103 Value: c
index: 104 Value: h
index: 105 Value: i
index: 106 Value: e
index: 107 Value: s
Explanation
In the above example, we have replaced the list with a string. Since string is iterable thus rest of the operation remains the same.
Python Enumerate Dictionary
We don’t use enumerate() function with Dictionaries & Sets because Dictionaries and Sets are not in sequence.
Conclusion
The enumerate() function is a built-in function available in python. This function has two parameters ‘iterable’ and ’start’. ‘iterable’ can be a list, tuple , string etc. ‘Start’ is optional and it’s default value is 0. This function returns an enumerate object. This object can also be iterated using for loop.