enumerate python Function

[Total: 1   Average: 5/5]

A lot of times when dealing with iterators, we also get a 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 enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.

Example

x = ('apple', 'banana', 'cherry')
y = enumerate(x)

print(list(y))

Output

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

 

 

admin
Leave a Reply

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages