How to access the Index in a For Loop in Python?

Spread the love

Problem –

You want to access the index when using a for loop in python.

Solution –

To access the index in a For loop in Python, You can use enumerate().

In [1]: lst = [2, 4, 6, 8, 10]

In [2]: for idx, item in enumerate(lst):
   ...:     print(idx, item)
   ...:     
0 2
1 4
2 6
3 8
4 10

Rating: 1 out of 5.

Leave a Reply