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.