Free tools Windows power users keep installed
One-click scans. No signup required.
Use for item in items: to visit a Python list’s values in order. If you also need each item’s position, use enumerate(items). For paired lists, reverse traversal, sorting, or filtering, Python’s iteration helpers make the intent clearer and help avoid common mistakes.
Loop through list values
When your task depends on each value rather than its position, iterate over the list directly:
items = ["apple", "banana", "cherry"]
for item in items:
print(item)
The loop visits items in their sequence order. You do not need to manage a counter or look up each item by index. This is Python’s standard approach to iterating over a sequence such as a list or string, as described in the Python 3.14.8 control-flow tutorial.
Get each item’s index with enumerate()
Use enumerate() when the loop needs both a value and its position:
for index, item in enumerate(items):
print(index, item)
Counting starts at zero. To start at one—for example, when displaying numbered output—pass start=1:
for number, item in enumerate(items, start=1):
print(number, item)
enumerate() works with iterables generally, not just lists. It is usually clearer than maintaining a separate counter when you need index-value pairs.
Use an index when the position drives the work
If the index itself is needed for a calculation or to access neighboring positions, loop over the valid index range:
for index in range(len(items)):
print(items[index])
range(len(items)) produces indices from zero up to, but not including, the list length. For simply processing every index and its value, prefer enumerate(items).
Choose an iteration helper for the order or pairing you need
| Need | Pattern | What it does |
|---|---|---|
| Pair corresponding values from multiple iterables | zip(first, second) |
Yields grouped items for each position. |
| Visit a sequence in reverse order | reversed(items) |
Traverses the sequence from the end toward the beginning. |
| Visit values in sorted order | sorted(items) |
Returns a sorted list without changing the original list. |
For example, zip() is useful when two lists hold related values at the same positions:
names = ["Mina", "Ravi"]
scores = [92, 85]
for name, score in zip(names, scores):
print(name, score)
To traverse backwards or in sorted order:
for item in reversed(items):
print(item)
for item in sorted(items):
print(item)
The Python 3.14.8 data-structures tutorial demonstrates these helpers, including that sorted() leaves the source sequence unchanged.
Filter into a new list instead of removing items mid-loop
Changing a list while iterating over it can make the loop’s behavior difficult to reason about. To keep only matching values, build a separate list:
filtered = []
for value in items:
if keep(value):
filtered.append(value)
This way, the loop traverses the original list while the result is built independently. The official data-structures tutorial recommends creating a new list as a simpler, safer alternative to changing a list during traversal.
What Python does during a for loop
A list is an iterable: Python can obtain an iterator from it, then request one item at a time until no items remain. The iterator protocol uses __iter__() and __next__(); when the iterator is exhausted, __next__() raises StopIteration. The Python 3.14.8 built-in types documentation describes the iterator protocol.
This is why the same for syntax also works with strings, files, generators, and other iterables. An iterator advances as it is consumed and generally does not rewind. If you need to traverse a one-shot iterator again, obtain a fresh iterator from its iterable when possible.
Iterating over a dictionary is different
A loop over a dictionary visits keys:
for key in mapping:
print(key)
Use mapping.values() to visit values or mapping.items() to get key-value pairs:
for value in mapping.values():
print(value)
for key, value in mapping.items():
print(key, value)
Dictionary iteration order is guaranteed to follow insertion order starting with Python 3.7, according to the Python 3.14.8 Functional Programming HOWTO.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesQuick Recap
Common list-iteration mistakes
- Using indexes without needing them: use
for value in items:when position does not matter. - Expecting the stop value in
range(): the endpoint is excluded, sorange(len(items))ends at the last valid index. - Removing or inserting values in the list being traversed: build a new list or deliberately iterate over a copy when that matches the task.
- Expecting a dictionary loop to yield values: direct dictionary iteration yields keys.
- Reusing an exhausted iterator: iterators advance rather than automatically restarting; request a fresh iterator for another pass.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




