All IT Courses 50% Off
Python Tutorials

Enumerate in Python with EXAMPLES

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
Facebook Comments

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.

Related Articles

Back to top button
Close
Close