Skip to main content

Iterator

Pronunciation
IT-uh-ray-ter
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/iterator

In short

An iterator is an object that lets code step through the items of a collection one at a time, remembering its position, without exposing how the data is stored.

What is an iterator?

An iterator is an object that produces the elements of a sequence one by one. Each time you ask for the next item, it returns that item and moves forward, and when nothing is left, it signals that it is finished. Iterators are how for...of loops in JavaScript, for loops in Python and foreach loops in C# and Java walk through collections.

Most languages define a small protocol for this. In Python, an iterable is anything with an __iter__ method that returns an iterator, and the iterator's __next__ method returns items until it raises StopIteration. In JavaScript, an iterator's next() method returns objects like { value: 1, done: false }, and Java and Rust have similar Iterator interfaces. Because items are produced on demand, iterators can walk over huge files, database results or even infinite sequences without loading everything into memory, and generator functions written with yield are a convenient way to create them.

An iterator is like a bookmark moving through a book: it knows where you are, gives you the next page when asked and doesn't care whether the book is a paperback or an e-book. This is the idea behind the iterator design pattern, which lets the same loop work with arrays, linked lists, trees, sets or streamed data, because the loop only talks to the iterator and never to the underlying data structure.

Iterators are often confused with iterables. An iterable is the collection that can be looped over, such as a list, while an iterator is the one-time cursor created from it that tracks the current position. You can loop over a list many times, but an iterator is usually used up after one pass, which surprises people who try to reuse it.

Key takeaways

  • An iterator returns items one at a time and remembers its current position.
  • An iterable is the collection; an iterator is the cursor created from it.
  • Loops such as for...of and Python's for use iterators behind the scenes.
  • Iterators can produce items lazily, which supports huge or infinite sequences.
  • Most iterators are consumed after one pass and must be recreated to loop again.

Example

Iterators and generators in Pythonpython
colors = ["red", "green", "blue"]   # an iterable

it = iter(colors)                   # create an iterator from it
print(next(it))  # red
print(next(it))  # green

# A generator function produces a lazy iterator
def countdown(n):
    while n > 0:
        yield n
        n -= 1

print(list(countdown(3)))  # [3, 2, 1]

Readers ask

What is the difference between an iterator and an iterable?

An iterable is something you can loop over, such as a list or a string. An iterator is the object that actually does the stepping, returning one item at a time and keeping track of where it is.

What is the difference between an iterator and a generator?

A generator is a convenient way to create an iterator by writing a function that uses yield. Every generator is an iterator, but iterators can also be written by hand as classes that implement the iteration protocol.

Why can't I loop over an iterator twice?

An iterator keeps an internal position, and once it has reached the end it stays there. To loop again, create a fresh iterator from the original collection.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings