How to access the index in for loops in Python?

Better Stack Team
Updated on January 24, 2023

In python, if you are enumerating over a list using the for loop, you can access the index of the current value by using enumerate function.

 
my_list = [1,2,3,4,5,6,7,8,9,10]

for index, value in enumerate(my_list):
    print(index, value)

Simply wrap the list you want to iterate over using the enumerate function and you can access not only the value but the index as well.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack