Python enumerate() Function with Examples

In the previous article, we have discussed Python divmod() Method with Examples
enumerate() Function in Python:

The enumerate() function accepts a collection (for example, a tuple) and returns an enumerate object.

The enumerate() function adds a counter as the enumerate object’s key.

Syntax:

enumerate(iterable, start)

Parameter Values

iterable: It is an iterable object.

start: This is optional. It is a number. Specifying the enumerate object’s starting number. 0 is the default.

Return Value:

The method enumerate() adds a counter to an iterable and returns it. The object returned is an enumerate object.

Enumerate objects can be converted to list and tuple using the list() and tuple() methods, respectively.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']

Output:

The given list after applying enumerate() function :
[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]

Example2:

Input:

Given List = ['good', 'morning', 'btechgeeks']
Given start value = 5

Output:

The given list after applying enumerate() function from the given start :
[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]

enumerate() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving start value

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list to the enumerate() function as an argument that returns an enumerate object. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks']
# Pass the given list to the enumerate() function as an argument that returns
# an enumerate object. The enumerate() function adds a counter as the
# enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst)
print("The given list after applying enumerate() function :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

The given list after applying enumerate() function :
[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]
2)With giving start value

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number(start value) as static input and store it in another variable.
  • Pass the given list, start value to the enumerate() function as arguments that returns an enumerate object from the given start value. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['good', 'morning', 'btechgeeks']
# Give the number(start value) as static input and store it in another variable.
gvn_strt = 5
# Pass the given list, start value to the enumerate() function as the arguments
# that returns an enumerate object from the given start value. The enumerate()
# function adds a counter as the enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst, gvn_strt)
print("The given list after applying enumerate() function from the given start :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

The given list after applying enumerate() function from the given start :
[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Pass the given list to the enumerate() function as an argument that returns an enumerate object. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list to the enumerate() function as an argument that returns
# an enumerate object. The enumerate() function adds a counter as the
# enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst)
print("The given list after applying enumerate() function :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

Enter some random List Elements separated by spaces = 1000 2000 3000 4000
The given list after applying enumerate() function :
[(0, 1000), (1, 2000), (2, 3000), (3, 4000)]
2)With giving start value

Approach:

  • Give the list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Give the number(start value) as user input using the int(input()) function and store it in another variable.
  • Pass the given list, start value to the enumerate() function as arguments that returns an enumerate object from the given start value. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number(start value) as user input using the int(input()) function 
# and store it in another variable.
gvn_strt = int(input("Enter some random number = "))
# Pass the given list, start value to the enumerate() function as the arguments
# that returns an enumerate object from the given start value. The enumerate()
# function adds a counter as the enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst, gvn_strt)
print("The given list after applying enumerate() function from the given start :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

Enter some random List Elements separated by spaces = 1 2 3 4 5
Enter some random number = 100
The given list after applying enumerate() function from the given start :
[(100, 1), (101, 2), (102, 3), (103, 4), (104, 5)]

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.