Skip to content Skip to sidebar Skip to footer

Iteration Count In Python?

Let's say I have a list of tuples l, and I do something like this: for (a,b) in l: do something with a,b, and the index of (a,b) in l Is there an easy way to get the index of

Solution 1:

Use enumerate.

for i, (a, b) in enumerate(l):
    # i will be the index of (a, b) in l

Solution 2:

Use enumerate():

for i,(a,b) in enumerate(l):
   ... # `i` contains the index

Post a Comment for "Iteration Count In Python?"