Enumerate() In Python

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/enumerate-in-python/
PINTEREST
PINTEREST
INSTAGRAM

Python provides easy to use functions and mechanisms for programmers. While dealing with collection or list types we generally need some way to enumerate them. enumerate() function is used to create an iterable or enumerable type from given list or collection. After this operation we can use created object to iterate with for or while structures.

Simply Enumerate

We will start with a simple enumerate example. We will provide a list named fruits to the enumerate() function which will change list with index numbered list in a tuple format.

Simply Enumerate
Simply Enumerate

Enumerate with Counter

We can specify the counter explicitly by specifyng start parameter like below. As we can see in the following example the start index will be 1 not default  .

Enumerate with Counter
Enumerate with Counter

Get Index Values

As enumerate() functions returns in tuple format we can get index and item into separate variables and use them in a loop like below.

Get Index Values
Get Index Values

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/enumerate-in-python/
PINTEREST
PINTEREST
INSTAGRAM
LEARN MORE  How To Loop and Iterate Over Python Dictionary?