Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python List Iterator: How It Works and When to Use It

Learn how Python list iterators work, how for loops use them, and when to prefer iterators over index-based access.

~3 min readPython
#python#iterator protocol#list iteration#for loops
Illustration of a Python list iterator moving through a list of elements, showing the next() pointer advancing.

When you call iter() on a list, Python returns a list iterator. The iterator has a __next__() method that returns the next element and raises StopIteration when the list is exhausted. This object is what allows a for loop, a comprehension, or functions such as map() and filter() to consume a list through a single, common protocol.

Creating and Using a List Iterator

To create a list iterator manually, call iter() with the list. Then call next() to fetch elements one at a time. If the iterator is exhausted, next() raises StopIteration.

python
fruits = ['apple', 'banana', 'cherry'] it = iter(fruits) print(next(it)) # apple print(next(it)) # banana print(next(it)) # cherry # next(it) would raise StopIteration

A list iterator keeps a reference to the original list and remembers its current position as it advances. It does not copy the list. Because it references a mutable list, changes to that list while iterating can change which elements are produced.

How For Loops Use List Iterators

When Python runs for item in my_list:, it calls iter(my_list) to get an iterator, then calls next() on that iterator until StopIteration is raised. The loop handles the exception for you, so you do not need to manage the protocol manually.

python
for fruit in fruits: print(fruit)

This protocol is not list-specific. Any object that supports __iter__() and __next__() can be used in a for loop. A list is iterable because calling iter() on it returns a new iterator; the list itself is not an iterator. That is why you can iterate over a list many times, while an iterator can only be consumed once.

Memory and Performance Characteristics

A list iterator is lightweight: it stores a reference to the list and a position in the list, rather than copying the list. Using a for loop with the iterator is often cleaner than maintaining an index in a while loop. However, a list iterator does not make list processing lazy. The list is already in memory, so if your goal is to avoid materializing a full collection, a generator or another lazy data source is the more appropriate tool.

If you need random access by position, an iterator is not suitable because it only moves forward. If you only need to process each element once, an iterator is idiomatic and keeps the iteration logic inside one looping construct.

Common Mistakes and Edge Cases

A common mistake is reusing an exhausted iterator. After StopIteration is raised, calling next() again keeps raising StopIteration. To iterate again over the original list, create a fresh iterator with iter().

Another edge case is modifying a list while iterating over it. If you add or remove elements, the iterator's remembered position can point to a different item than intended, causing some elements to be skipped or processed more than once. The safest approach is to iterate over a copy with original[:] or collect the changes and apply them after the loop.

Remember the difference between an iterable and an iterator: a list is iterable but not an iterator. Calling iter() on an iterator returns the same iterator, which reinforces that an iterator can be used only once.

When to Use an Iterator Instead of Indexing

Use indexing when you need the index itself or need access to elements non-sequentially. Use an iterator when you want to process each element once and write code that works with any iterable, such as a tuple, set, generator, or custom iterable. Functions like map(), filter(), and zip() accept iterables, so an iterator can be handy there as well.

If a function accepts an iterable, passing an iterator is fine, but be aware that the iterator is consumed in the process. If two consumers need the same values, keep the original list and call iter() before each consumer, or convert the iterator's output to a list with list(iterator).

Compatibility with Other Iterable Types

The iterator protocol is not specific to lists. Any object that implements __iter__() and __next__() can be used with the same iteration syntax. Lists, tuples, strings, dictionaries, sets, files, and many custom objects can be iterated with for. Understanding how list iterators work gives you a foundation for working with all of those types.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.