Python for loop to process list items

A Python for loop steps through each item in an iterable, so python for loop searches usually mean “repeat this work per item.” Use it when you need to apply the same rule to every item in a list or sequence and collect results along the way, like totals, flags, or a clean summary list. It keeps repeated work readable without manual indexing.

Python For Loop Example For Iterating Items

Output:

Output will appear here...

Output:

Done: ship order
Done: email customer

How This Example Works

The loop walks one item at a time so you can apply the same rules consistently.

  1. for task in tasks pulls each string from the list.
  2. The same print action runs for every item.
  3. The output proves the loop ran once per task.

Common Mistakes

Mistake 1: Expecting range to include the end value.

# Wrong: expects 1 through 5
for day in range(1, 5):
    print(day)
# Right: range stops before the end value
for day in range(1, 6):
    print(day)

This happens because range is end-exclusive, so the stop value is never produced. For more, see the Python range example.

Mistake 2: Modifying a list while iterating it.

# Wrong: removing items shifts the list and can skip users
for user in users:
    if user["inactive"]:
        users.remove(user)
# Right: build a new list instead
active_users = []
for user in users:
    if not user["inactive"]:
        active_users.append(user)

Removing items changes the list while the loop is still walking it, so some elements are never visited.

Mistake 3: Misreading for ... else.

# Wrong: expects else to run after a break
for item in items:
    if item["id"] == target_id:
        break
else:
    print("Item not found")
# Right: else runs only when the loop finishes without break
for item in items:
    if item["id"] == target_id:
        print("Item found")
        break
else:
    print("Item not found")

The else block only executes when the loop completes normally, so a break skips it.

for loop vs while: Which to Use

Use for when…Use while when…
You have a list, range, file, or any iterable to process.You are waiting for a condition to change (retries, polling, user input).
The number of iterations is determined by the data.The number of iterations is unknown ahead of time.
You want clean iteration over items.You need manual control over when to stop.

Rule of thumb: use for for “for each item” work, use while for “until this condition” work.

Performance Considerations

A for loop is O(n) over the items you give it. range() is lazy, so iterating large numeric ranges does not allocate a giant list in memory. If you only need the first match, break early or use next/any with a generator to short-circuit the scan and avoid needless work.

More Python for loop patterns

Add position labels with enumerate.

tasks = ["ship order", "email customer", "update CRM"]
for index, task in enumerate(tasks, start=1):
    print(f"{index}. {task}")

Use enumerate when you need both the item and its position, without manual counters. Related: Python enumerate example.

Summarize values from a dictionary with .items().

region_sales = {"west": 1200, "central": 950, "east": 1420}
for region, total in region_sales.items():
    print(f"{region}: ${total}")

Looping over .items() keeps keys and values together, which is ideal for reports. See the Python dict example for key/value lookups.

When to Use a Python for loop

  • You need to apply the same rule to every order, user, or file line.
  • You want a clear, readable pass over items without manual index management.
  • You need to collect results (totals, lists of issues) as you iterate.
  • Avoid it when you only need a single match; short-circuit with next, any, or all instead.