I was working on a Python project where I needed to loop through a list of U.S. states and display both the index and the name. The challenge was simple, but it reminded me how often beginners struggle with accessing the index while looping in Python.
In my 10+ years of experience as a Python developer, I’ve seen this question come up countless times: “How can I use a for loop with an index in Python?”
The good news? There are several clean, Pythonic ways to do this. In this tutorial, I’ll walk you through five practical methods to use Python for loop with index, each with clear examples and explanations.
Method 1 – Use enumerate() in Python
The easiest and most Pythonic way to loop through items with their index is by using the enumerate() function.
When you use enumerate(), Python automatically keeps track of the index for you, so you don’t have to manually manage it.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
for index, state in enumerate(states):
print(f"Index {index}: {state}")This code prints both the index and the state name for each item in the list.
The output will look like this:
Index 0: California
Index 1: Texas
Index 2: Florida
Index 3: New York
Index 4: IllinoisI executed the above example code and added the screenshot below.

If you want to start counting from a number other than zero, you can pass a second argument to enumerate().
for index, state in enumerate(states, start=1):
print(f"State {index}: {state}")This will print the states starting from index 1 instead of 0, perfect for user-facing data or reports.
Method 2 – Use range() with len() in Python
Before enumerate() became popular, many developers (including myself) used the range() and len() combination to loop through lists with an index.
This method is still useful when you need both the index and the item but want to control the loop manually.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
for i in range(len(states)):
print(f"Index {i}: {states[i]}")I executed the above example code and added the screenshot below.

This approach gives the same output as before. However, it’s slightly less readable than enumerate() and not as “Pythonic.” Still, it’s good to know because it’s often used in legacy codebases and interviews.
Method 3 – Use zip() with range() in Python
If you like flexibility and want to combine multiple lists or add more control, you can use the zip() function along with range().
This method is clean and works well when you want to loop through multiple lists simultaneously while keeping track of the index.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
abbreviations = ["CA", "TX", "FL", "NY", "IL"]
for i, (state, abbr) in enumerate(zip(states, abbreviations)):
print(f"{i}: {state} ({abbr})")I executed the above example code and added the screenshot below.

This method is great for real-world applications, like matching state names with abbreviations or product names with IDs.
Method 4 – Use Python while Loop with a Counter
Sometimes, you may want more control over the iteration process. In such cases, using a while loop with a manual counter can be a good option.
This approach is more verbose, but it helps you understand how indexing works internally in Python.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
index = 0
while index < len(states):
print(f"Index {index}: {states[index]}")
index += 1I executed the above example code and added the screenshot below.

While this approach works fine, it’s not as efficient or elegant as enumerate() or range(). I’d recommend using it only when you need to manipulate the index dynamically during iteration.
Method 5 – Use NumPy’s ndenumerate() (For Arrays)
If you’re working with NumPy arrays, you can use the built-in ndenumerate() function to loop through array elements with their indices.
This is particularly useful in data science, machine learning, or numerical computing projects.
import numpy as np
data = np.array([[10, 20, 30], [40, 50, 60]])
for index, value in np.ndenumerate(data):
print(f"Index {index}: Value {value}")This will print both the row and column index for each element in the array. It’s an excellent way to iterate over multi-dimensional data efficiently in Python.
Bonus Tip – Accessing Index in List Comprehensions
Python list comprehensions are a concise way to create lists, but they don’t directly support indexes. However, you can combine them with enumerate() to achieve the same result.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
indexed_states = [f"{i}: {state}" for i, state in enumerate(states)]
print(indexed_states)Output:
['0: California', '1: Texas', '2: Florida', '3: New York', '4: Illinois']This one-liner is perfect for quick data transformations or generating formatted strings.
When to Use Each Method
Here’s a quick summary of when to use each method:
| Method | Best Used For | Pythonic? |
|---|---|---|
enumerate() | Every day loops with an index | ✅ Yes |
range() + len() | Legacy code or manual index control | ⚠️ Moderate |
zip() + range() | Looping through multiple lists | ✅ Yes |
while loop | Dynamic or conditional looping | ⚠️ Moderate |
np.ndenumerate() | Working with NumPy arrays | ✅ Yes |
As a rule of thumb, use enumerate() whenever possible; it’s clean, efficient, and widely accepted in the Python community.
Common Mistakes When Using For Loop with Index in Python
- Modifying the list while looping:
Avoid changing the list (adding or removing elements) inside the for loop. It can cause unexpected behavior or index errors. - 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:
for i, value in enumerate(my_list):
...Real-World Example – Loop Through U.S. States Data
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.
states = ["California", "Texas", "Florida", "New York", "Illinois"]
population = [39538223, 29145505, 21538187, 20201249, 12812508]
for index, (state, pop) in enumerate(zip(states, population), start=1):
print(f"{index}. {state} - Population: {pop:,}")Output:
1. California - Population: 39,538,223
2. Texas - Population: 29,145,505
3. Florida - Population: 21,538,187
4. New York - Population: 20,201,249
5. Illinois - Population: 12,812,508This example feels more practical and relatable, especially if you’re analyzing U.S. demographic data or creating reports.
Key Takeaways
- The enumerate() function is the best and most Pythonic way to use a for loop with an index.
- The range() and len() combo is still useful in older codebases.
- Use zip() when you need to loop through multiple lists at once.
- For numerical and matrix data, NumPy’s ndenumerate() is your go-to method.
- Always write clean, readable, and efficient Python code.
Using a Python for loop with an index is one of those small but powerful skills that make your code cleaner and more professional.
Whenever I work on data analysis, automation scripts, or even web scraping projects, I find myself using enumerate() almost daily. It’s elegant, readable, and built for exactly this purpose.
I hope this tutorial helped you understand all the different ways you can loop with an index in Python. Try each method, experiment with your own data, and you’ll quickly see which one fits your coding style best.
You may like to read:
- Use the Floor() Function in Python
- Use Built-In Functions in Python
- Difference Between Functions and Methods in Python
- Pass a Function as a Parameter in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.