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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why use iterators

A
  • memory efficient
  • process one item at a time
  • avoid generating/copying whole collection
  • access each item in turns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Iterable

A

something that could conceptually be accessed element-by-element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Iterator

A

actual interface allowing element by element access

use iterator to access iterable object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly