Enumerate in Python

 

Code


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.
The 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 the list() method.


>>> l = [i for i in range(1, 100, 10)]

>>> l

[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

>>> 

>>> for i, j in enumerate(l):

print("i is :{}, j is :{}".format(i, j))


i is :0, j is :1

i is :1, j is :11

i is :2, j is :21

i is :3, j is :31

i is :4, j is :41

i is :5, j is :51

i is :6, j is :61

i is :7, j is :71

i is :8, j is :81

i is :9, j is :91






Quiz Level 1

Quiz Level 2


Request Me

Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python