Migrate to RunxBuild and earn up to $50 in hosting credit on your first deposit.

Calculate your savings
unxBuild

Python Iterate a List: for, enumerate, list comprehension, and map

Sean

Platform Writer

Jul 07, 2026
5 min read

Python iterate a list with for item in lst (the default), for i, item in enumerate(lst) (when you need the index), [f(x) for x in lst] (when you want a new list), or map(f, lst) (when you want a lazy iterator). The team that picks the right one writes idiomatic Python; the team that uses for i in range(len(lst)) is writing Java in Python.

Python Iterate a List: for, enumerate, list comprehension, and map

Table of contents

The default: for item in lst


fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

    print(fruit)

Output:


apple

banana

cherry

This is the right pick for 90% of iteration needs. The team that needs the index adds enumerate.

When you need the index: enumerate


fruits = ["apple", "banana", "cherry"]

for i, fruit in enumerate(fruits):

    print(i, fruit)

Output:


0 apple

1 banana

2 cherry

The enumerate function returns (index, item) tuples. The team that needs the index uses this. Avoid for i in range(len(lst)): lst[i] - that is the Java way and Python has the right idiom.

To start counting from 1: enumerate(fruits, start=1). The team that has 1-indexed line numbers in a log file uses this.

List comprehension: build a new list


numbers = [1, 2, 3, 4, 5]

squared = [n ** 2 for n in numbers]

# squared = [1, 4, 9, 16, 25]

List comprehensions are Python’s idiomatic way to build a new list by transforming each element. They are faster than for + append (because the list size is pre-allocated), and they read like a math expression.

With a condition:


evens = [n for n in numbers if n % 2 == 0]

# evens = [2, 4]

The team that uses list comprehensions for simple transformations writes cleaner code. The team that crams a 10-line transformation into a single comprehension is overdoing it - use a regular loop for complex logic.

map: lazy iteration


numbers = [1, 2, 3, 4, 5]

squared = map(lambda n: n ** 2, numbers)

# squared is a map object (iterator), not a list

list(squared)  # [1, 4, 9, 16, 25]

map returns an iterator, not a list. The elements are computed on demand. The team that processes a huge list and does not need all the elements in memory uses map for memory efficiency.

Compare to list comprehension: [n**2 for n in numbers] is a list, map(lambda n: n**2, numbers) is an iterator. For small lists, use whichever reads better. For large lists where you do not need all elements, use map or a generator expression (n**2 for n in numbers).

Iterating with index and value: enumerate + start

Sometimes you need the index in a specific format:


for i, item in enumerate(items, start=1):

    print(f"Item {i}: {item}")

Or when iterating multiple lists in parallel:


names = ["Alice", "Bob", "Charlie"]

ages = [30, 25, 35]

for name, age in zip(names, ages):

    print(f"{name} is {age}")

zip stops at the shorter list. For unequal-length lists, use itertools.zip_longest.

Modifying the list while iterating

Don’t. The team that modifies a list while iterating it gets skipped elements or IndexError. The right pattern: iterate over a copy, or build a new list.


# Wrong - skips elements

for item in items:

    if condition(item):

        items.remove(item)



# Right - iterate over a copy

for item in items[:]:

    if condition(item):

        items.remove(item)



# Right - build a new list

items = [item for item in items if not condition(item)]

FAQ

What is the difference between a list and a generator?

A list holds all elements in memory. A generator computes each element on demand. For a million-element list, the list takes megabytes of RAM; the generator takes constant memory. The team that iterates a huge file uses a generator.

Is enumerate faster than range(len(lst))?

Marginally. enumerate is implemented in C, so it is faster than the equivalent Python code with range(len(...)). The team that benchmarks finds enumerate is 5-15% faster, and more readable.

Can I modify a list while iterating?

Not safely. Modifying a list while iterating it can skip elements or raise IndexError. The right pattern is to iterate over a copy (for item in lst[:]) or build a new list.

What is the difference between map and a list comprehension?

List comprehensions return a list. map returns an iterator. For most cases they are interchangeable; the team that prefers the list-comprehension syntax uses that, the team that wants lazy iteration uses map or a generator expression.

Should I use for loops or while loops for iteration?

Almost always for. while is for when the loop condition is not ‘iterate over this sequence’ (e.g., wait for a condition to be true, retry until success). The team that uses while True: ... if done: break instead of for ... in ... is missing the right tool.

How this fits the rest of the stack

For a sense of what the full project costs before it commits, the RunxBuild hosting calculator shows the line items together. The API, the database, the storage, the worker, the bandwidth - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers.

Useful related references:

#python#list#iteration#loops