What are the uses of the "enumerate()" function on Python?


In this article, we will learn about the enumerate() function and what are the uses of the "enumerate()" function in Python.

What is enumerate() function?

Python's enumerate() function accepts a data collection as a parameter and returns an enumerate object.

The enumerate object is returned in key-value pair format. The key is the corresponding index of each item, and the value is the items.

Syntax

enumerate(iterable, start)

Parameters

  • iterable − the data collection that is passed in so that it can be returned as enumerate object is called iterable

  • start − as the name suggests, the starting index of an enumerate object is defined by start. If we ignore this value it will consider the first index as zero, as zero is the default value here

Uses of enumerate() function in Python

When is the enumerate() function used?

The enumerate() function is used if they want the index of iterate values.

How is it used?

The enumerate function assigns an index of each iterable value to the list.

Why is the enumerate() function used?

When working with iterators, we always need to know how many iterations have been completed i,e, the count of iterations.

However, we have a pythonic approach of determining the number of iterations for necessary. The syntax is as follows −

enumerate(iterable, start)

How to Use enumerate() function in Python?

It is important to note which data type will be used to store the enumerated object after it is returned.

Example

The following program uses enumerate() function on lists to get a list of tuples with indices −

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object of the input list
enumerate_obj = enumerate(inputList)
print(enumerate_obj)

# converting the enumerate object into a list and printing it
print(list(enumerate_obj))

Output

On executing, the above program will generate the following output −

<enumerate object at 0x7f4e56b553c0>
[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

Using the second parameter ‘start index’ of the enumerate() function

Example

The following program shows how the enumerate() function is applied on a list including the second parameter −

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object starting from the index 15
enumerate_obj = enumerate(inputList, 15)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

Output

On executing, the above program will generate the following output −

[(15, 'hello'), (16, 'tutorialspoint'), (17, 'python'), (18, 'codes')]

In the above example, we have added 15 as the second parameter in the enumerate() function. This second parameter will indicate the starting index in the enumerator object for the keys(indices), hence we can see the first index is 15 followed by the second index as 16 and so on in the output.

Python code to loop through an enumerate object

Example

The following program shows how to loop through an enumerate object using a for loop and print each element of it −

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object
enumerate_obj = enumerate(inputList)

# traversing through each item in an enumerate object
for i in enumerate_obj:

    # printing the corresponding iterable item
    print(i)

Output

On executing, the above program will generate the following output −

(0, 'hello')
(1, 'tutorialspoint')
(2, 'python')
(3, 'codes')

Using enumerate() function on a Tuple

Example

The following program shows how to use enumerate() function on the given input tuple −

# input tuple
inputTuple = ("hello", "tutorialspoint", "python", "codes")

# getting the enumerate object of the input tuple
enumerate_obj = enumerate(inputTuple)
print(enumerate_obj)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

Output

On executing, the above program will generate the following output −

<enumerate object at 0x7fec12856410>
[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

Using enumerate() function on String

Iterate through the string data using the enumerate function to determine the index of each character.

Example

The following program shows how to use enumerate() function on the given input string −

# input string
inputString = "python"

# getting the enumerate object of an input string
enumerate_obj = enumerate(inputString)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

Output

On executing, the above program will generate the following output −

[(0, 'p'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]

Using Another Parameter in enumerate() function

Example

The following program shows how to use another parameter in enumerate() function −

input_data = ('Hello', 'TutorialsPoint', 'Website')
for count, dataValue in enumerate(input_data,start = 50):
   print(count, dataValue)

Output

On executing, the above program will generate the following output −

(50, 'Hello')
(51, 'TutorialsPoint')
(52, 'Website')
  • Enumerate is a Python built-in function that returns an enumerate object.

  • This function returns an enumerate object.

  • To pass the iterable object to the enumerate function, first attempt to retrieve data from the enumerate object and then convert it to a list(). As a result, as seen in the example above, it returns a list of tuples along with the iterate index.

Conclusion

The applications of Python's enumerate() function were covered in great length in this article. We also learned how to use it with various Python datatypes and iterables. Additionally, we learned how to modify the enumerate() function's beginning count or index.

Updated on: 16-Jan-2023

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements