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. Answer from waspbr on reddit.com
🌐
Stack Abuse
stackabuse.com › how-to-access-index-in-pythons-for-loop
How to Access Index in Python's for Loop
January 6, 2021 - This is the most common way of ... code which illustrates how this method is used: my_list = [3, 5, 4, 2, 2, 5, 5] print("Indices and values in my_list:") for index, value in enumerate(my_list): print(list((index, value)))...
🌐
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. ... # create a list of fruits fruits = ['apple', 'banana', 'orange'] print("Indices and Index value :") # Iterate over the indices of the list and access elements using indices for i in range(len(fruits)): print(f"Index: {i}, Value: {fruits[i]}")
Discussions

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
October 5, 2022
using the 'enumerate' function to iterate over a list with index and value
Enumerate is a generator. Like when you call range() in a loop. They yield values. More on reddit.com
🌐 r/pythontips
4
10
March 25, 2024
python - How can I access the index value in a 'for' loop? - Stack Overflow
The fastest way to access indexes of list within loop in Python 3.7 is to use the enumerate method for small, medium and huge lists. Please see different approaches which can be used to iterate over list and access index value and their performance metrics (which I suppose would be useful for ... More on stackoverflow.com
🌐 stackoverflow.com
python - Loop through list with both content and index - Stack Overflow
Are there any more elegant/Pythonic ways of doing this? ... >>> S = [1,30,20,30,2] >>> for index, elem in enumerate(S): print(index, elem) (0, 1) (1, 30) (2, 20) (3, 30) (4, 2) ... Sign up to request clarification or add additional context in comments. ... This is the answer. Just providing the link like in the selected answer is not "StackOverflow-esc" 2016-03-25T03:19:58.603Z+00:00 ... I am sending data using ajax to Django views as an array in the form of two values ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
You can access the index even without using enumerate(). Using a for loop, iterate through the length of my_list. Loop variable index starts from 0 in this case. In each iteration, get the value of the list at the current index using the statement value = my_list[index]. Print the value and index.
🌐
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.

🌐
Mimo
mimo.org › glossary › python › enumerate-function
Python enumerate Function: Master Index-Based Iteration
The enumerate() function in Python provides a cleaner way to access both the index and the value of elements during iteration. Using enumerate() allows you to access both the item and its index in a list. In Python, enumerating lists can be useful whenever you need the index and the value within ...
Find elsewhere
🌐
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 positions.
🌐
Sentry
sentry.io › sentry answers › python › accessing the index in a `for` loop in python
Accessing the Index in a `for` Loop in Python | Sentry
November 22, 2022 - Let’s say we want to print the ... of start to 1, like so: directions = ['north', 'east', 'south', 'west'] for index, direction in enumerate(directions, start=1): print(f"{index} {direction}") ... There are other ways of ...
🌐
Trey Hunner
treyhunner.com › 2016 › 04 › how-to-loop-with-indexes-in-python
How to loop with indexes in Python
April 25, 2016 - 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....
🌐
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.
🌐
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.
🌐
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
🌐
StrataScratch
stratascratch.com › blog › mastering-loop-iterations-python-for-loop-index-explained
Mastering Loop Iterations: Python For Loop Index Explained - StrataScratch
October 17, 2024 - This loop prints the client’s ID and age for the first five rows in the client_data.csv dataset. By using break, we control how much data is shown. The for loop index refers to the position of the current element in the sequence during each iteration of a for loop. Sometimes, you may want to access the index along with the value of each component while iterating.
🌐
Python Morsels
pythonmorsels.com › looping-with-indexes
Looping with indexes - Python Morsels
October 8, 2020 - Often when you're trying to loop with indexes in Python, you'll find that you actually care about counting upward as you're looping, not actual indexes. Let's say we have a variable, favorite_fruits that points to a list of strings: >>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] ... >>> n = 1 >>> for fruit in favorite_fruits: ... print(n, fruit) ... n += 1 ... 1 jujube 2 pear 3 watermelon 4 apple 5 blueberry · We're printing out n and fruit (which represents each of our fruits).
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)
🌐
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
🌐
Flexiple
flexiple.com › python › python-program-accessing-index-value-list
Python Program To Accessing Index And Value In A List - Flexiple
March 18, 2024 - The zip(indices, fruits) pairs each index with its corresponding fruit. The loop then prints each pair. This method is particularly useful when you need to iterate over a list and you also need to keep track of the indices of the elements. The zip() function keeps the code clean and readable, making it a preferred choice for many Python programmers. ... Accessing index and value in a list using numpy.ndenumerate() is an efficient method, particularly when dealing with multi-dimensional arrays in Python.