Sometimes, we want to access the index in Python for loops.
In this article, we’ll look at how to access the index in Python for loops.
How to access the index in Python for loops?
To access the index in Python for loops, we can use the enumerate
function.
For instance, we write:
ints = [100, 200, 300]
for idx, val in enumerate(ints):
print(idx, val)
to call enumerate
with the ints
array to return an array with the idx
and val
tuples.
idx
is the index of the array entry.
val
is the value of the array entry.
Therefore, we see:
0 100
1 200
2 300
logged as a result.
Conclusion
To access the index in Python for loops, we can use the enumerate
function.