Python enumerate() Tutorial

Python Enumerate() Function

This is a detailed tutorial of python enumerate() function. Learn to give number counts to different iterable objects like a list in python.

Python enumerate() Function

In python, enumerate() is a very useful function that can automatically assign sequenced numbering to different iterable objects. This function will return an Enumerate Type object, which can be easily iterated using Python For Loop or While Loop. You can also directly convert this enumerate type object into a more readable iterable like Python List.

Syntax

The syntax of the enumerate() function is given below.

Arguments

It can take two arguments. The first one has to be an iterable object and the second one should be the numerical index that you want to assign to the first member of the provided iterable or the number from which it should start enumerating the iterable elements.

The second parameter, start is optional and if you do not provide it as a parameter, the enumeration will start from index 0 as it is the default value of start.

Return Type

The return type of this function is an enuerateObject, which is also iterable and we’ve already discussed how you can easily traverse it using different loops or simple sequence data types such as a list.

Python Enumerate Type Object Example

The above screenshot shows that the return type of the function enumerate() is an enumerate object.

Examples

The usage of the enumerate() function is simple. The following examples give a quick look at how easily you can use this function to give sequenced numbering to different iterables.

Basic Usage of enumerate() Function

We’ve used this function in the example by providing a non-numbered list and a non-numbered tuple to convert them into a numbered list and tuple.

Python Enumerate() Function Example

In the above code, I’ve defined a list and a tuple, both of which do not have any numbered indexes. Then I applied the function enumerate() on both of these iterables, one without start parameter and one with start parameter as 5. As we know this function will return an enumerate type object.

So, as we can convert into any iterable, I converted the vegetables numbered iterable object into a list and fruits numbered iterable object to a numbered tuple. Therefore, the variable vegetables which originally was a list is now a numbered tuple and the variable fruits which originally was a tuple is now a numbered list.

Doing iteration using enumerate()

You can also do numbered iteration using the enumerate() function easily. The following example illustrates the same.

Python Enumerate() Function Iteration Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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