Use the built-in function enumerate():

for idx, x in enumerate(xs):
    print(idx, x)

It is non-Pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable.

Check out PEP 279 for more.

Answer from Mike Hordecki on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_loop.asp
Python - Loop Lists
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration.
Top answer
1 of 16
9260

Use the built-in function enumerate():

for idx, x in enumerate(xs):
    print(idx, x)

It is non-Pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable.

Check out PEP 279 for more.

2 of 16
1370

Using a for loop, how do I access the loop index, from 1 to 5 in this case?

Use enumerate to get the index with the element as you iterate:

for index, item in enumerate(items):
    print(index, item)

And note that Python's indexes start at zero, so you would get 0 to 4 with the above. If you want the count, 1 to 5, do this:

count = 0 # in case items is empty and you need it after the loop
for count, item in enumerate(items, start=1):
    print(count, item)

Unidiomatic control flow

What you are asking for is the Pythonic equivalent of the following, which is the algorithm most programmers of lower-level languages would use:

index = 0            # Python's indexing starts at zero
for item in items:   # Python's for loops are a "for each" loop 
    print(index, item)
    index += 1

Or in languages that do not have a for-each loop:

index = 0
while index < len(items):
    print(index, items[index])
    index += 1

or sometimes more commonly (but unidiomatically) found in Python:

for index in range(len(items)):
    print(index, items[index])

Use the Enumerate Function

Python's enumerate function reduces the visual clutter by hiding the accounting for the indexes, and encapsulating the iterable into another iterable (an enumerate object) that yields a two-item tuple of the index and the item that the original iterable would provide. That looks like this:

for index, item in enumerate(items, start=0):   # default is zero
    print(index, item)

This code sample is fairly well the canonical example of the difference between code that is idiomatic of Python and code that is not. Idiomatic code is sophisticated (but not complicated) Python, written in the way that it was intended to be used. Idiomatic code is expected by the designers of the language, which means that usually this code is not just more readable, but also more efficient.

Getting a count

Even if you don't need indexes as you go, but you need a count of the iterations (sometimes desirable) you can start with 1 and the final number will be your count.

count = 0 # in case items is empty
for count, item in enumerate(items, start=1):   # default is zero
    print(item)

print('there were {0} items printed'.format(count))

The count seems to be more what you intend to ask for (as opposed to index) when you said you wanted from 1 to 5.


Breaking it down - a step by step explanation

To break these examples down, say we have a list of items that we want to iterate over with an index:

items = ['a', 'b', 'c', 'd', 'e']

Now we pass this iterable to enumerate, creating an enumerate object:

enumerate_object = enumerate(items) # the enumerate object

We can pull the first item out of this iterable that we would get in a loop with the next function:

iteration = next(enumerate_object) # first iteration from enumerate
print(iteration)

And we see we get a tuple of 0, the first index, and 'a', the first item:

(0, 'a')

we can use what is referred to as "sequence unpacking" to extract the elements from this two-tuple:

index, item = iteration
#   0,  'a' = (0, 'a') # essentially this.

and when we inspect index, we find it refers to the first index, 0, and item refers to the first item, 'a'.

>>> print(index)
0
>>> print(item)
a

Conclusion

  • Python indexes start at zero
  • To get these indexes from an iterable as you iterate over it, use the enumerate function
  • Using enumerate in the idiomatic way (along with tuple unpacking) creates code that is more readable and maintainable:

So do this:

for index, item in enumerate(items, start=0):   # Python indexes start at zero
    print(index, item)
Discussions

How do I iterate through a list by index using a list for the index?
Hint: If you do for i in indices:, what are the values of i? More on reddit.com
๐ŸŒ r/learnpython
15
1
May 2, 2023
Why iterate over an array using the index?
You do it if there is something in your loop that will use an index rather than the elements of the array. For instance, sometimes you want to sample something from another array that is not your loop array A cleaner way to do it is by using enumerate function for i, item in enumerate(array): The index gets assigned to i and whatever element of the array gets assigned to item. This may also be a bad habit from Matlab, people that come from Matlab are more used to looping over indexes. More on reddit.com
๐ŸŒ r/learnpython
57
46
September 26, 2022
Is it possible to iterate over a list of lists in a function?
You could use a nested list comprehension, eg. def func(*args): return [ele for sublist in zip(*args) for ele in sublist] This type of operation is known as flattening a list. In this case we are flattening the output of zipping all the sublists. More on reddit.com
๐ŸŒ r/learnpython
16
6
May 31, 2021
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-loop-through-a-list-using-the-index-numbers-in-python
How to loop through a list using the index numbers in Python
We can loop through a list in Python by referring to the index numbers of the elements of the list. In this case, all we need are the range() and len() functions to create a suitable iterable...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ index-for-loop
Python Program to Access Index of a List Using for Loop
You can give any name to these variables. Print the required variables inside the for loop block. The function of enumerate() is to add a counter (i.e. index) to the iterate and return it. my_list = [21, 44, 35, 11] for index, val in enumerate(my_list, start=1): print(index, val)
๐ŸŒ
Trey Hunner
treyhunner.com โ€บ 2016 โ€บ 04 โ€บ how-to-loop-with-indexes-in-python
How to loop with indexes in Python
We could use range(len(our_list)) and then lookup the index like before: But thereโ€™s a more idiomatic way to accomplish this task: use the enumerate function. Pythonโ€™s built-in enumerate function allows us to loop over a list and retrieve ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-iterate-with-index-in-python-419446
How to iterate with index in Python | LabEx
def safe_index_access(data, index, default=None): try: return data[index] except IndexError: return default sample_list = [10, 20, 30] print(safe_index_access(sample_list, 5, "Not Found")) Indexed iteration enables sophisticated data manipulation ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
Here we are using a while loop to iterate through a list. We first need to find the length of list using len(), then start at index 0 and access each item by its index then incrementing the index by 1 after each iteration.
Published ย  December 27, 2025
Find elsewhere
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ enumerate
Python enumerate() - Iterate With Index | Vultr Docs
December 5, 2024 - Define a string. Iterate through the string using enumerate() to get each character along with its index. ... text = "hello" for index, char in enumerate(text): print(f"Character {char} at position {index}") Explain Code ยท This example demonstrates ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-iterate-over-a-list-and-pull-element-indices-at-the-same-time
How to iterate over a list and pull element indices at the same time - Quora
Answer (1 of 8): Adding to the correct answer by User-12748610390850255192, there is an optional start argument to the enumerate function, which I find very helpful when I need to count from 1 or any other number instead of 0. [code]for index, value in enumerate(numbers, start=1): print 'The ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-iterate-over-list-with-index
Iterate over List using Index in Python
1__ initialize variable x, with ... of Python Examples, we learned how to iterate over the items of a list with access to index as well using enumerate() function, with the help of well detailed examples....
๐ŸŒ
Vultr
docs.vultr.com โ€บ python โ€บ examples โ€บ access-index-of-a-list-using-for-loop
Python Program to Access Index of a List Using for Loop | Vultr Docs
November 25, 2024 - Start counting from a non-zero index by passing a second argument to enumerate(). Iterate over the list and print each item's index starting from your chosen number.
๐ŸŒ
Real Python
realpython.com โ€บ python-enumerate
Python enumerate(): Simplify Loops That Need Counters โ€“ Real Python
June 23, 2025 - Pythonโ€™s enumerate() function helps you with loops that require a counter by adding an index to each item in an iterable. This is particularly useful when you need both the index and value while iterating, such as listing items with their ...
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ mastering-loop-iterations-python-for-loop-index-explained
Mastering Loop Iterations: Python For Loop Index Explained - StrataScratch
October 17, 2024 - Python's for loops are user-friendly but can drag with large datasets. Alternatives like vectorized operations in pandas often run faster. Using iloc[] or at[] inside loops can be costly because pandas shine with vectorized tasks, not explicit looping. If you must use it for loops, try enumerate(). It automatically tracks the index as you iterate, cutting out manual handling.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-iterate-list-with-index
Python: Iterating Over Lists with Index - CodeRivers
March 26, 2025 - The enumerate() function simplifies ... use the index obtained during iteration to modify elements in the list. For example, let's say we want to square every even - indexed element in a list....
๐ŸŒ
YouTube
youtube.com โ€บ case digital
How To Iterate Through A List With Index In Python - YouTube
In this python tutorial, I answer the question of how to iterate through a list with index in python! If fact I'll show you two different ways you can iterat...
Published ย  January 13, 2023
Views ย  876
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-for-loop-with-index
Learn Python: For Loops With Index (With Examples)
June 7, 2024 - In this example, zip(indices, fruits) returns a list of tuples, where each tuple contains an index and a fruit. The for loop then iterates over these tuples, and the variables index and fruit are set to the index and fruit of each tuple respectively.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ ways to iterate through list in python
Ways to Iterate Through List in Python - AskPython
January 16, 2024 - Pythonโ€™s range() method can be used in combination with a for loop to traverse and iterate over a list in Python. The range() method basically returns a sequence of integers i.e. it builds/generates a sequence of integers from the provided start index up to the end index as specified in the argument list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ access-the-index-and-value-using-python-for-loop
Access the Index and Value using Python 'For' Loop - GeeksforGeeks
July 23, 2025 - ... # create a list of fruits fruits ... list and access elements using indices for i in range(len(fruits)): print(f&quot;Index: {i}, Value: {fruits[i]}&quot;)...