View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Often, when dealing with iterators, we also get need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. Enumerate() method adds a counter to an iterable and returns it in a form of enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() method. Syntax: enumerate(iterable, start=0) Parameters:
Example Python3
Output: Return type: [(0, 'eat'), (1, 'sleep'), (2, 'repeat')] [(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')] Using Enumerate object in loops: Python3
Output: (0, 'eat') (1, 'sleep') (2, 'repeat') 100 eat 101 sleep 102 repeat 0 eat 1 sleep 2 repeat This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or have more information about the topic discussed above. What is the use of enumerate function in Python?The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.
What is the use of enumerate () function?Definition and Usage
The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.
What is enumerate in pandas?The enumerate() function is a built-in function that returns an enumerate object. This lets you get the index of an element while iterating over a list. In other programming languages (C), you often use a for loop to get the index, where you use the length of the array and then get the index using that.
What is an enumeration Python?By definition, an enumeration is a set of members that have associated unique constant values. Enumeration is often called enum. Python provides you with the enum module that contains the Enum type for defining new enumerations. And you define a new enumeration type by subclassing the Enum class.
|