October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Iterate Through a List in Python

Use a direct for loop for list values, enumerate() for index-value pairs, and Python’s iteration helpers for paired, reverse, sorted, or filtered traversal.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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, so range(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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.