ArticlesPython

How to use enumerate() in Python


Enumerate function in Python

The enumerate() function can be extremely useful when developing code.

The built-in function applies to collection data types, such as lists and tuples. It converts the iterable to an enumerate object, adds a counter (indexing) for each item in the iterable, and then displays the result. The result outputs the enumerate object, instead of the actual outcome of the function. For this reason, we need to display the result with casting – e.g. list(), tuple(), etc.

It has two arguments:

  • iterable (compulsory)
  • start index (optional)

The first arguments is the collection. It must be specified, as the function needs to know what to apply to.

The second arguments is the start index. This is optional, as the default value is 0.

Examples of Python enumerate()

Let’s have a look at some examples of the function.

  1. Create a list of four names (John, Jessica, Peter, and Samantha).
  2. Create the enumerate object and store it in a variable (names_enum)
  3. Print the result, with the help of casting (list(names_enum))
names = ["John", "Jessica", "Peter", "Samantha"]

names_enum = enumerate(names)

print(tuple(names_enum))

# Output: [(0, 'John'), (1, 'Jessica'), (2, 'Peter'), (3, 'Samantha')]

Let’s try the above scenario with a tuple.

names = ("John", "Jessica", "Peter", "Samantha")

names_enum = enumerate(names)

print(tuple(names_enum))

# Output: ((0, 'John'), (1, 'Jessica'), (2, 'Peter'), (3, 'Samantha'))

We can also display the output with a for loop.

The outcome of the for loop is more readable, as each item prints on a new line.

We use the above tuple example.

names = ("John", "Jessica", "Peter", "Samantha")

names_enum = enumerate(names)

for i in names_enum:
    print(i)

Here’s the output.

(0, 'John')
(1, 'Jessica')
(2, 'Peter')
(3, 'Samantha')

Enumerate with a for loop

The function of enumerate() can also be implemented directly inside the for loop.

This method removes the step of storing the enumerate object in a variable.

Below is an example.

names = ("John", "Jessica", "Peter", "Samantha")

for index, item in enumerate(names):
    print(index, item)
0 John
1 Jessica
2 Peter
3 Samantha

Enumerate vs standard methods

The function of enumerate offers an easy and quick way of creating iterable counters.

We can also achieve this with a for loop, without the use of enumerate.

Let’s compare the difference between the different approaches.

Counter with a for loop

names = ["John", "Jessica", "Peter", "Samantha"]

counter = 0

for i in names:
    print(counter, i)
    counter += 1
0 John
1 Jessica
2 Peter
3 Samantha

Counter with the range() and len() functions

names = ["John", "Jessica", "Peter", "Samantha"]

counter = 0

for i in range(len(names)):
    item = names[i]
    print(i, item)
0 John
1 Jessica
2 Peter
3 Samantha

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.