Menu Close

Python enumerate() Function

In this tutorial, we are going to learn all about Python enumerate() function to add a counter as the key of the enumerate object. In the previous tutorial, we have seen lots of python built-in functions that are used to perform various operations.

Python enumerate() function

The Python enumerate function takes an iterable ( list, tuple, set ) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.

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 start number of the enumerate object. Default is 0.

Return Value

enumerate function in python return list that contains tuples which contain the counter and iterable item.

Python enumerate examples

Here, we will see some examples to understand python enumerate method.

Example 1:

Use python enumerate method with single parameter.

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

Output

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

Example 2:

Use python enumerate method with both first and second parameter.

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

Output

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

Iterate enumerate object

Use for loop to iterate through enumerate 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

In this tutorial, you have learned the enumerate function in python to add counter as the key of an enumerate object. You can use any iterable ( list, tuple, set ) object with enumerate function in python.

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

Other Python built-in functions

For more information:- Click Here

Python divmod() Function
Python eval() Function

Was this post helpful?

Related Posts

Leave a Reply

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