# Iterator

> An object that hands out items one at a time, each time you ask.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** Iterators, Iterable, Iterator Protocol

## Think of it like

It is like a bookmark in a book. It always knows which page comes next.

## Definition

An iterator is an object that hands out the items of a collection one at a time. Each call to next gives the next item. When there are no more, it says it is done. A for loop uses an iterator behind the scenes. Lists, strings and files can all be stepped through this way.

## Example

Jack reads a huge log file line by line with an iterator. Only one line is in memory at a time.

## Example code

```python
it = iter(["apple", "pear"])
print(next(it))  # apple
print(next(it))  # pear
next(it)         # StopIteration
```

Each next() gives one item. After the last, it signals it is done.

## Why it matters

It lets one loop work over many kinds of collection. It also lets you walk through data too big to load at once.

## Common mistake

Looping over the same iterator twice. Once it is used up, it gives nothing the second time.

## Three principles

- **One at a time.** Each call gives the next item.
- **Knows its place.** It remembers where it stopped.
- **Runs out.** Most cannot be rewound.

## Related terms

- [Loop](https://learn.benibix.com/term/development/loop)
- [For Loop](https://learn.benibix.com/term/development/for-loop)
- [Array](https://learn.benibix.com/term/development/array)
- [Generator (Programming)](https://learn.benibix.com/term/development/generator)
- [Object (Programming)](https://learn.benibix.com/term/development/object)

## Told apart from

- [Iterator vs Generator](https://learn.benibix.com/compare/iterator-vs-generator): An iterator is any object that gives items one at a time. A generator is a short way to write one: a function that pauses at each yield.

![Two panels. On the left, an iterator: a list of three fruits with a pointer that moves one step on each call to next, and stops after the last. On the right, a generator: a function with a yield line hands out 1, then pauses, then 2, then 3, making each value only when asked.](https://learn.benibix.com/diagrams/dev7-iterator-generator.svg)

---

Source: https://learn.benibix.com/term/development/iterator
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
