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
Remember to increase the index by 1 after each iteration. Print all items, using a while loop to go through all the index numbers · thislist = ["apple", "banana", "cherry"] i = 0 while i < len(thislist): print(thislist[i]) i = i + 1 Try it Yourself » · Learn more about while loops in our Python While Loops Chapter.
🌐
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)
Top answer
1 of 16
9259

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
1368

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 - ... text = "hello" for index, char in enumerate(text): print(f"Character {char} at position {index}") Explain Code · This example demonstrates how to print each character of the string "hello" with its corresponding index, facilitating operations ...
🌐
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;)...
🌐
Trey Hunner
treyhunner.com › 2016 › 04 › how-to-loop-with-indexes-in-python
How to loop with indexes in Python
For example, let’s say we’re printing out president names along with their numbers (based on list indexes). 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 both the index and the value of each item in the list: The enumerate function gives us an iterable ...
🌐
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
The combination of the range() and len() functions, (range(len(iterable), is used to iterate over a list in Python. Using the two functions together, we can refer to the index numbers of all items or elements in a list.
🌐
LabEx
labex.io › tutorials › python-how-to-iterate-with-index-in-python-419446
How to iterate with index in Python | LabEx
LabEx recommends practicing these techniques to master Python iteration · Practical index iteration goes beyond basic examples, solving complex programming challenges with elegant solutions. def filter_by_index(data, condition): return [item for index, item in enumerate(data) if condition(index)] numbers = [10, 20, 30, 40, 50, 60] even_indexed_numbers = filter_by_index(numbers, lambda idx: idx % 2 == 0) print(even_indexed_numbers) ## Output: [10, 30, 50] def sync_list_operations(list1, list2): result = [] for index, (item1, item2) in enumerate(zip(list1, list2)): result.append((index, item1 *
Find elsewhere
🌐
Real Python
realpython.com › python-enumerate
Python enumerate(): Simplify Loops That Need Counters – Real Python
June 23, 2025 - In the example above, you’re using enumerate() to get the index i and pet in your loop. Then, you use the index to access the item in owners. The code works but it’s a bit complicated because you’re creating i just to work with the index. Instead, it would be cleaner to aggregate both lists with zip() and loop through the iterator that the function returns:
🌐
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
🌐
Python Examples
pythonexamples.org › python-iterate-over-list-with-index
Iterate over List using Index in Python
1__ initialize variable x, with a list of three strings 3__ write a for loop, where we iterate over the, enumerated x 3__enumerate(x)__ enumerate of x, gives us an iterator, to iterate over the index and item at a time 3__index, item__ we have access to index and item during each iteration 4__ with having access to both index and item, print them to output using print method ... In this tutorial 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.
🌐
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 = ["java", "python", "HTML", "PHP"] print("Indices:", [i for i in range(len(data))]) print("Elements:", [data[i] for i in range(len(data))]) ... Explanation: [i for i in range(len(data))] creates a list of indices.
🌐
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.
🌐
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.
🌐
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 - In the below example, enumerate(courses) returns pairs of index and value for each element in the list. The for loop then iterates over these pairs, and you can use the index variable to access the index of the current element.
🌐
Python Guides
pythonguides.com › python-for-loop-index
How to Use Python For Loop with Index
October 14, 2025 - Using wrong start index: Remember that Python uses zero-based indexing. If you want a human-readable index, start from 1 using enumerate(states, start=1). Forgetting tuple unpacking: When using enumerate(), make sure to unpack both the index and the item correctly: ... Let’s say you have a list of states and their populations, and you want to print them in a formatted way with their index.