Enumerate() Method in Python

Enumerate means to state or mention several things one by one. While iterations are being performed it sometimes becomes necessary to count the number of iterations. In python, there exists a built-in function to do so.
In this function, it adds counter to an iterable. The function then returns the enumerated object.

Syntax: Enumerate() in Python

The function takes 2 parameters: Iterable, start

enumerate(iterable, start=1)
  1. Iterable: An object that is used for supporting iteration. It is also known as the iterator.
  2. Start: This parameter is optional. As shown in the above syntax it provides value from which the counter is to be initiated. (In this case 1)
    If no value is provided it takes zero as the initial value.

Example of enumerate() method in Python

  • The enumerated objects can be converted into Lists
food = ['cheesecake', 'pancakes', 'fries', 'burger', 'pizza']
enumerateFood = enumerate(food)

# converting to list
print(list(enumerateFood))

# changing the default counter
enumerateFood = enumerate(food, 100)
print(list(enumerateFood))

Output:

[(0, 'cheesecake'), (1, 'pancakes'), (2, 'fries'), (3, 'burger'), (4, 'pizza')]
[(100, 'cheesecake'), (101, 'pancakes'), (102, 'fries'), (103, 'burger'), (104, 'pizza')]

In this example, first, the enumerated objects are converted into a list without mentioning the value of start variable. Therefore the list starts with zero. Then the enumerated objects are again converted into a list but this time the start value is provided i.e 100. Therefore, the list starts with 100.

  • The enumerated objects can be converted into Tuples
food = ['cheesecake', 'pancakes', 'fries', 'burger', 'pizza']
enumerateFood = enumerate(food)

# converting to tuple
print(tuple(enumerateFood))

# changing the default counter
enumerateFood = enumerate(food, 100)
print(tuple(enumerateFood))

Output:

((0, 'cheesecake'), (1, 'pancakes'), (2, 'fries'), (3, 'burger'), (4, 'pizza'))
((100, 'cheesecake'), (101, 'pancakes'), (102, 'fries'), (103, 'burger'), (104, 'pizza'))

In this example, just like the example above the enumerated objects are first converted into a tuple without mentioning the start value, therefore the iteration starts from 0. Then the enumerated objects are converted into a tuple. The iteration in which the starts from 100.

  • The enumerated objects can be used in loops.
food = ['cheesecake', 'pancakes', 'fries', 'burger', 'pizza']
enumerateFood = enumerate(food)

for i in enumerateFood:
  print(i)

print('\n')
for count, i in enumerateFood:
  print(count, i)

# changing default start value
for count, i in enumerate(food, 10):
  print(count, i)

Output:

(0, 'cheesecake')
(1, 'pancakes')
(2, 'fries')
(3, 'burger')
(4, 'pizza')


10 cheesecake
11 pancakes
12 fries
13 burger
14 pizza

In this example, the enumerated objects are used in for loop. First, the initial value or value of start is not mentioned therefore the default value i.e 0 is used. Then the value of start is mentioned i.e 10 in this case therefore the iteration starts with 10.

Leave a Reply

Your email address will not be published. Required fields are marked *