Enumerate in Python with EXAMPLES

Table of Contents

Sometimes you want to reference your items in the list or other data structure for later use. It makes easier by providing an enumerate function in Python. 

Let’s take a look at the parameter of enumerate.

enumerate(iterable, startIndex)

  • Iterable: list or other iterable.
  • StartIndex: It is the starting number. StartIndex is optional.

Let’s take a look at the code.

name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]
e_name = enumerate(name)
print(e_name)
Enumerate in Python

The enumerate function returns an enumerate object that we need to iterate to get the output values. Let us iterate through the list.

name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]
e_name = enumerate(name)
for i in e_name:
print(i)
Enumerate in Python

Now let’s start counting from 5 now.

name = [‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’]
e_name = enumerate(name,5)
for i in e_name:
  print(i)

The following is the output

Enumerate in Python

Enumerating a Tuple

The enumerate on tuple works the same as on lists.

name = (‘Alex’, ‘Bob’ ,’Celvin’, ‘Dexter’)
e_name = enumerate(name)
for i in e_name:
print(i)
Enumerating a Tuple

Enumerating a String

Let’s take a look at the code to enumerate in python string.

name = (‘Hello’)
e_name = enumerate(name)
for i in e_name:
print(i)
Enumerating a String

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class