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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
a = [1, 3, 5, 7, 9] # Here, i and val reprsents index and value respectively for i, val in enumerate(a): print (i, val) ... List comprehension is similar to for loop. It provides the shortest syntax for looping through list. ... a = [1, 3, 5, 7, 9] # On each iteration val is passed to print function # And printed in the console.
Published   December 27, 2025
🌐
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 - Below are some of the examples by which we can access the index value in Python: ... In this method, we are using the range() function to generate indices and access values in a list by their position.
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)
🌐
Vultr Docs
docs.vultr.com › python › built-in › enumerate
Python enumerate() - Iterate With Index | Vultr Docs
December 5, 2024 - Use the enumerate() function to iterate through the list, obtaining both index and value.
🌐
Programiz
programiz.com › python-programming › examples › index-for-loop
Python Program to Access Index of a List Using for Loop
my_list = [21, 44, 35, 11] for index in range(len(my_list)): value = my_list[index] print(index, value) ... You can access the index even without using enumerate(). Using a for loop, iterate through the length of my_list.
🌐
Reddit
reddit.com › r/pythontips › using the 'enumerate' function to iterate over a list with index and value
r/pythontips on Reddit: using the 'enumerate' function to iterate over a list with index and value
March 25, 2024 -

Suppose you want to iterate over a list and access both the index and value of each element.

You can use this code:

# Original list
lst = ['apple', 'banana', 'cherry', 'grape']

# Iterate over the list with index and value
for i, fruit in enumerate(lst):
    print(f"Index: {i}, Value: {fruit}")

# Output
# Index: 0, Value: apple
# Index: 1, Value: banana
# Index: 2, Value: cherry
# Index: 3, Value: grape

The enumerate function returns a tuple containing the index and value of each element, which can be unpacked into separate variables using the for loop.

Find elsewhere
🌐
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
Line 5: We create a for loop and, using the range(len(iterable)) function, we refer to respective index numbers of all the items present in the list and create an iteration over all the items.
🌐
Trey Hunner
treyhunner.com › 2016 › 04 › how-to-loop-with-indexes-in-python
How to loop with indexes in Python
Python’s built-in enumerate function ... each item in the list: The enumerate function gives us an iterable where each element is a tuple that contains the index of the item and the original item value....
🌐
iO Flood
ioflood.com › blog › python-for-loop-with-index
Learn Python: For Loops With Index (With Examples)
June 7, 2024 - In Python, a for loop is used to iterate over a sequence such as a list, tuple, dictionary, string, etc. It’s a control flow statement, which allows code to be executed repeatedly.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python – access index in for loop with examples
Python - Access Index in For Loop With Examples - Spark By {Examples}
May 31, 2024 - You can use the enumerate() function in a for loop to access both the elements and their corresponding indices. For example, the enumerate() function is used in the for loop to iterate over both the indices and values of the courses.
🌐
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....
🌐
Replit
replit.com › home › discover › how to iterate through a list in python
How to iterate through a list in Python | Replit
February 12, 2026 - This is the most straightforward way to perform an action on every element within an iterable. Beyond the basic for loop, other techniques give you more control—like using a while loop or enumerate()—or offer a concise way to transform data. fruits = ["apple", "banana", "cherry"] index = 0 while index < len(fruits): print(fruits[index]) index += 1--OUTPUT--apple banana cherry
🌐
Real Python
realpython.com › python-enumerate
Python enumerate(): Simplify Loops That Need Counters – Real Python
June 23, 2025 - Learn how to simplify your loops with Python’s enumerate(). This tutorial shows you how to pair items with their index cleanly and effectively using real-world 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 - Iterate over the list while unpacking the index and value during each loop iteration. ... fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits): print(f"Index: {index}, Fruit: {fruit}") Explain Code
🌐
Educative
educative.io › answers › how-to-iterate-over-a-list-in-python
How to iterate over a list in Python
If not found, the loop completes normally, and the else block prints Not found. When you need both the index and the value of an element during iteration, the enumerate() function is the go-to solution for us.
🌐
Llego
llego.dev › home › blog › accessing elements by index during iteration in python
Accessing Elements by Index During Iteration in Python - llego.dev
May 25, 2023 - Provides concise and readable code by avoiding manual indexing inside loops. Overall, enumerate() should be your preferred approach when you need both the index and value during iteration.
🌐
TutorialKart
tutorialkart.com › python › how-to-iterate-over-a-list-with-index-in-python
How to Iterate Over a List with Index in Python
February 11, 2025 - To iterate over a list with index in Python, we can use methods like enumerate(), the range() function, list comprehension, or zip(). These approaches allow accessing both the index and the corresponding value of each element in the list during ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-access-index-in-for-loop-python
How to Access Index using for Loop - Python - GeeksforGeeks
July 23, 2025 - [data[i] for i in range(len(data))] creates a list of values by index. The zip() function can combine two lists: one with indices and one with elements, allowing simultaneous iteration.