Menu Close

Python enumerate() Function

Python enumerate() tutorial

In this article, you will learn everything about Python enumerate() function to return a enumerate object. The enumerate() function in Python is one of the most important functions to add a counter to each item of the iterable. Throughout this article, we will explore Python enumerate() function along with various examples.

Python enumerate() function

Python enumerate() function is a built-in function which means it comes with Python by default. It takes an iterable ( list, tuple, set, etc ) and returns an enumerate object. A enumerate object is basically a collection of tuple and each tuple contain an item of the passed iterable and its counter value.

For instance, you can see below example:

[(0, 'India'), (1, 'Japan'), (2, 'Russia')]

Syntax

The syntax of enumerate() function in Python is:-

enumerate(iterable, start)

Parameter

enumerate function in Python accepts two parameters.

  • iterable:– An iterable object.
  • start:- A number. Define the starting number of the enumerated object. The default is 0.

Return Value

enumerate() function in Python return list that contains tuples that contain the counter and iterable item.

Python enumerate() Function examples

In this section, we will explore Python enumerate() function along with multiple examples so that you can understand this enumerate() function easily.

Note:- You have to keep one thing in your mind. To see the items of the enumerate object, you must convert it into list, tuple and set by using list(), tuple() and set() functions. Otherwise you can use Python for loop to iterate each item of the enumerate object.

Example: Using enumerate() function with a single parameter

I have a Python tuple x and now I will pass this tuple to enumerate function to return enumerate object.


x = ('apple', 'cherry', 'papaya')
y = enumerate(x)
print(list(y))

Output


[(0, 'apple'), (1, 'banana'), (2, 'cherry')]

Example: Converting a list to enumerate object

Here I am converting Python to enumerate objects.


mylist = ['Python', 'PHP', 'Java', 'C++', 'HTML', 'CSS']
result = enumerate(mylist)
print(list(result))

Output

[(0, 'Python'), (1, 'PHP'), (2, 'Java'), (3, 'C++'), (4, 'HTML'), (5, 'CSS')]

Example: Using enumerate() function with the second parameter

As we have seen in the parameter section, The enumerate() function takes the second parameter which represents the starting number of the counter. Let see.


students_name = ["Vishvajit", "Vikas", "Vinod", "Akash", "Veronika"]
enumerate_object = enumerate(students_name, start=5)
print(list(enumerate_object))

Output

[(5, 'Vishvajit'), (6, 'Vikas'), (7, 'Vinod'), (8, 'Akash'), (9, 'Veronika')]

As you can see in the above example, how we have started the counter from number 5.

Iterate enumerate object

You can also use Python for loop to iterate each item of the enumerated object.


mylist = ['Python', 'PHP', 'Java', 'C++', 'HTML', 'CSS']
result = enumerate(mylist, 5)
for i in result:
	print(i)

Output

(5, 'Python')
(6, 'PHP')
(7, 'Java')
(8, 'C++')
(9, 'HTML')
(10, 'CSS')

Conclusion

So, In this tutorial, We have successfully covered the enumerate() function in Python to add a counter as the key of an enumerate object. You can use any iterable ( list, tuple, set ) object with enumerate function in Python. This function is going to be very useful especially when you want to add a counter to each item of the iterable and write some business logic based on the counter number.

If this article helped you, please visit for further python built-in functions tutorials.

Other Python built-in functions


For more information:- Click Here

Python getattr() Function
Python format() Function

Related Posts