Javatpoint Logo
Javatpoint Logo

Python enumerate() Function

Python enumerate() function returns an enumerated object. It takes two parameters first is a sequence of elements and the second is the start index of the sequence. We can get sequence elements either through a loop or next() method.

The next() method of the iterator returned by enumerate() returns a tuple and the values obtained from iterating over a sequence.

Syntax:

Here is the syntax for the enumerate() function:

Parameters

sequence: It must be a collection or a sequence. It is the required parameter.

start: It is an optional index, used to set starting of the sequence.

Return

This method returns an object of the enumerated type.

Let's see some examples of enumerate() function to understand it's functionality.

Python enumerate() Function Examples:

Example 1:

This is an illustration of the way to utilize the enumerate() function:

Code:

Output:

0 apple
1 banana
2 cherry

In this model, the enumerate() function is utilized to emphasize over the fruits list, and for every component in the rundown, a tuple containing the index and the relating value is delivered. The for loop then prints each tuple, bringing about the result displayed previously.

Example 2:

One normal use case for the enumerate() function is the point at which you need to emphasize over a sequence and furthermore access the index of every component. For instance, assume you have a rundown of numbers and you need to track down the index of the principal negative number. You could utilize the enumerate() function to achieve this as follows:

Code:

Output:

The first negative number is -3 at index 2.

In this example, the enumerate() function is utilized to emphasize over the numbers list, and for every component in the rundown, a tuple containing the index and the comparing value is delivered. The in the event that assertion checks in the event that the value is negative, and assuming this is the case, it prints a message containing the index and value of the main negative number.

Example 3:

Another utilization case for the enumerate() function is the point at which you need to make a word reference that maps every component in a sequence to its index. You can utilize a word reference perception and the enumerate() function to achieve this as follows:

Code:

Output:

{'apple': 0, 'banana': 1, 'cherry': 2}

In this example, the enumerate() function is utilized to emphasize over the fruits list, and for every component in the rundown, a tuple containing the index and the comparing value is delivered. The word reference understanding makes a word reference where each key is a natural product name and each value is the relating index.

Conclusion:

In Conclusion, the enumerate() function in Python is a helpful method for repeating over a sequence and monitoring the index of every component. It tends to be utilized in various settings, from basic loops to additional perplexing tasks like making word references. This can be especially helpful when you need to access both the index and value of each element in a loop.


Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA