Iterators Flashcards
1
Q
Iterator
A
- keeps track of transversal of a container
- iterable object with return an iterator object when you pass it through iter() e.g.
iterator = iter([1,2,3,])` - keeps track of current index in iterable, end of iterable
2
Q
Iterator objects
A
_ _ next _ _ method, return next thing in the iteraiton and update their state/memory of where they are up to
next()
- iterator raises StopIteration exception when container is empty
3
Q
Why use iterators
A
- memory efficient
- process one item at a time
- avoid generating/copying whole collection
- access each item in turns
4
Q
Iterable
A
something that could conceptually be accessed element-by-element
5
Q
Iterator
A
actual interface allowing element by element access
use iterator to access iterable object
6
Q
Itertools
A
- implements number of iterator building blocks
- standardises set of fast, memory efficient tools
- each tool can be used alone or in combination
e. g. product, cycle, groupby, combinations