Enumerate means to mention a collection of things one by one.
Python has a builtin function enumerate() to enumerate an iterable. In this tutorial, we learn how enumeration helps us to iterate over an iterable with access to index of items, and how to enumerate over different types of objects like list, tuple, dictionary, set, string, etc.
Basically enumerate() function returns an enumerated object for the given iterable. And if we iterate over the enumerated object, the enumerated object throws us both the index and item in a tuple in each iteration.
How does enumerate() help?
If we use a For loop with an iterable, we can access only the items of the iterable inside the loop, but not the index of the items. Also, if we use a While loop, we have to initialize a counter for index and increment it in the iterations.
Enumerate helps to overcome this problem.
Using enumerate() with For loop, we can get access to both the index and the item in a concise code.
Looping over an iterable using For loop and the iterable itself makes it possible to access only the items, but not index.
for item in iterable: print(item)
Looping over an iterable using While loop requires us to initialize a variable to store the index. The code is not concise, but at least we get to have access to the index of item.
index = 0 while index < len(iterable): print(index, iterable[index]) index += 1
Now, let us use enumerate() function to access both the index and item in a For loop.
for index, item in enumerate(iterable): print(index, item)
Using enumerate() function makes the code, as concise as using a For loop, but as functional as a While loop, to iterate over the items of an iterable and have access to the index of items.
The enumerated object (object returned by enumerate() function) is an iterable by itself, and because of which we are able to use a For loop on this enumerated object.
enumerate() builtin function
Please refer enumerate() builtin function. This tutorial explains the syntax and usage of enumerate() function.
Enumerate a List
Let us take a list of strings in
myList and iterate over the enumerated object of this list. We print both the index and the item inside the loop.
myList = ['apple', 'banana', 'cherry'] for index, item in enumerate(myList): print(index, item)
0 apple 1 banana 2 cherry
Enumerate a Tuple
Let us take a tuple of items in
myTuple and iterate over the enumerated object of this tuple. We print both the index and the item inside the loop.
myTuple = ['apple', 25, 'India'] for index, item in enumerate(myTuple): print(index, item)
0 apple 1 25 2 India
Enumerate a String
String is a collection of characters. If we iterate over an enumerated string object, we iterate over the characters of the string, with access to the index of the character in the string.
Let us take a string in
myString and iterate over the characters of the string, with access to the index of characters in the string using enumerate().
myString = 'apple' for index, item in enumerate(myString): print(index, item)
0 a 1 p 2 p 3 l 4 e
Enumerate a Set
Enumerating a Set does not make much meaning, since the items in a set are not ordered and a Set is defined in that way. But we are not limited by that fact to enumerate a Set in Python.
Let us take a set of items in
mySet and iterate over the enumerated object of this set.
mySet = set(['apple', 'banana', 'cherry']) for index, item in enumerate(mySet): print(index, item)
0 cherry 1 banana 2 apple
Since we are enumerating a Set object, please note that the index of items may change when the program is rerun.
More Examples on Enumerate
The following tutorials cover more examples and special cases of using enumerate() in Python.
- Python – Enumerate a List
- Python – Enumerate a Tuple
- Python – Enumerate a Dictionary
- Python – Enumerate a String
- Python – Enumerate with different starting index
- Python – Enumerate start at 1
In this tutorial of Python Examples, we learned about Enumeration in Python, and how to enumerate a given iterable, with examples.