Python Enumerate

Enumerate

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 the index of items, and how to enumerate over different types of objects like lists, tuples, dictionaries, sets, strings, 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 the 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 the 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 an 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 this 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.

Python Program

myList = ['apple', 'banana', 'cherry']

for index, item in enumerate(myList):
    print(index, item)
Run Code Copy

Output

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.

Python Program

myTuple = ['apple', 25, 'India']

for index, item in enumerate(myTuple):
    print(index, item)
Run Code Copy

Output

0 apple
1 25
2 India

Enumerate a String

A 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().

Python Program

myString = 'apple'

for index, item in enumerate(myString):
    print(index, item)
Run Code Copy

Output

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.

Python Program

mySet = set(['apple', 'banana', 'cherry'])

for index, item in enumerate(mySet):
    print(index, item)
Run Code Copy

Output

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.

  1. Python – Enumerate a List
  2. Python – Enumerate a Tuple
  3. Python – Enumerate a Dictionary
  4. Python – Enumerate a String
  5. Python – Enumerate with different starting index
  6. Python – Enumerate start at 1

Summary

In this tutorial, we learned about Enumeration in Python, and how to enumerate a given iterable, with examples.

Code copied to clipboard successfully 👍