iterator
An object that produces the items of a sequence one at a time, on request, instead of all at once.
In more detail
A for loop works by asking an iterator for its next item until it is exhausted. Iterators let programs process huge files or endless streams one value at a time without loading everything. Anything that can hand out an iterator, such as a list or a file, is called an iterable.
Example
heapq.merge reads one value at a time from each sorted input.Related terms
- generator — A function that can pause and resume, producing a series of values one at a time with yield, each only when it is requested.
- k-way merge — Merging k sorted sequences into one by keeping the current front item of each in a min-heap and repeatedly taking the smallest.
- enumerator — The object that walks a sequence, with MoveNext to advance and Current to read the item it is on.
- deferred execution — Building a query now but running it only when something asks for its results.
- state machine — A model in which something is always in exactly one named state, such as idle, loading, success or error, and only defined events move it to another.
- IAsyncEnumerable<T> — A .NET interface for a sequence whose items arrive over time, consumed with await foreach.
- async iteration — Looping over values that arrive over time with for await (const x of source), waiting for each one before the next turn of the loop.
- iterable — Any object you can loop over with for, such as a list, string, tuple, dictionary, set, range, file or generator.
- next() — next(it) asks an iterator for its next item; iter(x) gets an iterator from an iterable.
- StopIteration — The exception an iterator raises from next() to say it has no more items.
Taught in
- Sorting In Real Systems: Stability, Keys And External Sort — Algorithms & Data Structures for Working Engineers
- Heaps And Priority Queues — Algorithms & Data Structures for Working Engineers
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- Stream Mechanics: Readable Streams & Flowing Modes — Node.js Internals & Backend Services
- Interactive While Loops — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book
- Comparing, Slicing And Never Changing — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- Python glossary: iterator
- Iterators
- yield statement
- MDN: Iteration protocols
- Built-in types: iterator types