Question
How can I access the index while iterating over a sequence with a for loop in Python?
For example, given this list:
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
I want the output to be:
item #1 = 8
item #2 = 23
item #3 = 45
What is the correct way to get the index value during the loop?
Short Answer
By the end of this page, you will understand how to get both the position and the value while looping through a list in Python. You will learn the recommended enumerate() approach, how it works, when to start counting from 0 or 1, and what mistakes to avoid.
Concept
In Python, a basic for loop gives you each item from a sequence one at a time:
for x in xs:
print(x)
This is great when you only care about the values. But sometimes you also need the index—the position of each item in the list.
A common beginner instinct is to try to use an index variable directly inside the loop, but Python does not create that variable automatically. You need to ask for it explicitly.
The standard Python way to get both the index and the value is enumerate():
for index, x in enumerate(xs):
print(index, x)
enumerate() wraps an iterable and produces pairs like:
(0, 8)
(1, 23)
(2, 45)
This matters because:
- it is clear and readable
- it avoids manual counter variables
- it works with many iterable objects, not just lists
- it follows common Python style
If you want counting to begin at instead of , use the argument:
Mental Model
Think of a list like a row of numbered lockers:
- the value is what is inside each locker
- the index is the locker number
A normal for loop hands you only the contents:
82345
But enumerate() hands you both the locker number and the contents at the same time:
- locker
0contains8 - locker
1contains23 - locker
2contains45
If you ask enumerate(..., start=1), it simply starts numbering the lockers from 1 for display purposes.
Syntax and Examples
Basic syntax
for index, value in enumerate(sequence):
print(index, value)
Start counting from 1
for index, value in enumerate(sequence, start=1):
print(index, value)
Example matching your output
xs = [8, 23, 45]
for index, x in enumerate(xs, start=1):
print("item #{} = {}".format(index, x))
Output:
item #1 = 8
item #2 = 23
item #3 = 45
Using f-strings instead of .format()
In modern Python, many developers prefer f-strings because they are shorter and easier to read.
xs = [, , ]
index, x (xs, start=):
()
Step by Step Execution
Consider this code:
xs = [8, 23, 45]
for index, x in enumerate(xs, start=1):
print(f"item #{index} = {x}")
Here is what happens step by step:
-
xsis created as a list containing three values:82345
-
enumerate(xs, start=1)prepares pairs of(index, value):(1, 8)(2, 23)(3, 45)
-
First loop iteration:
index = 1x = 8
Real World Use Cases
Getting the index during iteration is common in real programs.
Display numbered lists
tasks = ["Email client", "Fix bug", "Deploy app"]
for i, task in enumerate(tasks, start=1):
print(f"{i}. {task}")
Show row numbers in data processing
rows = ["Alice", "Bob", "Charlie"]
for row_num, name in enumerate(rows, start=1):
print(f"Row {row_num}: {name}")
Report errors with positions
values = [10, -2, 30]
for i, value in enumerate(values):
if value < 0:
print(f"Invalid value at index {i}: {value}")
Real Codebase Usage
In real codebases, developers often use enumerate() when they need both the item and its position without managing a separate counter.
Common patterns
Validation with position info
emails = ["a@example.com", "", "c@example.com"]
for i, email in enumerate(emails, start=1):
if not email:
print(f"Email missing at row {i}")
Logging progress
files = ["a.txt", "b.txt", "c.txt"]
for i, filename in enumerate(files, start=1):
print(f"Processing file {i}: {filename}")
Guard clauses inside loops
values = [5, None, 9]
for i, value in enumerate(values):
value :
()
(value * )
Common Mistakes
Mistake 1: Assuming index exists automatically
Broken code:
xs = [8, 23, 45]
for x in xs:
print(index, x)
Why it fails:
indexwas never defined- a plain
forloop only gives you the value
Fix:
for index, x in enumerate(xs):
print(index, x)
Mistake 2: Using range(len(xs)) when enumerate() is clearer
This works:
for i in range(len(xs)):
print(i, xs[i])
But this is usually better:
for i, x (xs):
(i, x)
Comparisons
Ways to get the index in Python
| Approach | Example | Good for | Drawbacks |
|---|---|---|---|
enumerate() | for i, x in enumerate(xs): | Most cases where you need index and value | None for normal use |
enumerate(..., start=1) | for i, x in enumerate(xs, start=1): | Human-friendly numbering | Display numbering is not the true zero-based index |
range(len(xs)) | for i in range(len(xs)): | Cases where you truly need numeric indexes | More verbose, less Pythonic |
.index() |
Cheat Sheet
# Get index and value
for i, x in enumerate(xs):
...
# Start counting from 1
for i, x in enumerate(xs, start=1):
...
Key rules
- A plain
for x in xs:loop gives only the value enumerate(xs)gives(index, value)pairs- Python indexes usually start at
0 - Use
start=1for display numbering
Recommended pattern
xs = [8, 23, 45]
for i, x in enumerate(xs, start=1):
print(f"item #{i} = {x}")
Avoid when possible
for i in range((xs)):
(i, xs[i])
FAQ
How do I get the index in a Python for loop?
Use enumerate():
for i, value in enumerate(xs):
print(i, value)
How do I start the index from 1 instead of 0?
Pass start=1 to enumerate():
for i, value in enumerate(xs, start=1):
print(i, value)
Is enumerate() better than range(len(list))?
Yes, in most cases. It is shorter, clearer, and less error-prone when you need both the index and the value.
Why is my index variable undefined?
Because Python does not create index automatically in a normal for loop. You must use enumerate() or define your own counter.
Can I use enumerate() with tuples, strings, or other iterables?
Mini Project
Description
Create a small numbered to-do list printer. This project demonstrates how to loop through a list while showing both the item number and the task text. It is a realistic example because many command-line tools and small scripts display numbered items for users.
Goal
Build a Python script that prints a numbered list of tasks using enumerate().
Requirements
- Create a list with at least three task names.
- Loop through the list and print each task with its number.
- Start numbering from
1instead of0. - Format the output so it looks clean and readable.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.