Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Iterator

Итератор

An iterator lets code take values one at a time without knowing how they are stored or produced. It keeps track of the traversal and signals when there is nothing left.

What an iterator is

Suppose a function needs to print every value it receives. It shouldn't need one loop for an array, another for a file, and a third for values calculated on demand. An iterator gives the function a common way to move through those sources. The code that uses the iterator asks for the next item; the iterator handles how to find or produce it.

Iterable versus iterator

An iterable is something you can ask for an iterator. An iterator tracks a particular traversal, including which value should come next. Two iterators created from the same list can move independently. But an iterable doesn't have to support a fresh pass: in Python, a generator object is its own iterator, so calling iter() on it does not rewind it.

The end-of-iteration signal depends on the language. Python's next(it) calls the iterator's __next__() method, which returns a value or raises StopIteration if the iterator is exhausted. A Python for loop calls iter() at the start and handles StopIteration from the iterator automatically. In JavaScript, an iterator's next() returns an object whose done flag tells the caller whether iteration has ended. In neither protocol should you guess that iteration is finished based on the value alone: None and undefined can be legitimate items.

Example: stopping partway through

numbers = [4, 7, 9]
it = iter(numbers)

for number in it:
    if number % 2:
        break

print(number)    # 7
print(next(it))  # 9

By the time the loop breaks, it has already taken 7 from the iterator. The next call therefore returns 9, not 7 or 4. The list itself hasn't changed. To start from the beginning, ask numbers for a new iterator.

When iterators help

Iterators are useful when you want to read a file line by line, visit nodes in a tree, or process records without gathering them all first. For example, a function looking for the first matching record can return as soon as it finds one. Whether this saves work or memory depends on the source: an iterator over a list does not make the list smaller, and collecting the entire output into another list removes the benefit of processing one item at a time.

Another common mistake is treating an iterator as a snapshot. Inserting or deleting items during traversal may cause elements to be skipped or trigger an error, depending on the collection and language. Check the collection's rules rather than assuming that the iterator isolates you from changes.

What interviewers may ask

You might be asked to explain why a helper that consumes an iterator leaves nothing for the caller's next loop, or to implement an iterator over a range. For an implementation, describe the state you keep, when it advances, and how you signal exhaustion. Test both an empty range and a range with one element: those cases quickly reveal whether you've misplaced the boundary check.