Python
Generators & Iterators
37 flashcards · answers and spaced-repetition review in the KnowCard app
What does this print?
```
gen = (x*x for x in range(3))
print(sum(gen))
print(sum(gen))
```
What does calling this line do?
```
def squares(n):
for i in range(n):
yield i*i
g = squares(3)
```
What is the key difference in behavior between [x for x in big] and (x for x in big)?
What does this print?
```
i = iter([10, 20])
print(next(i, 'done'))
print(next(i, 'done'))
print(next(i, 'done'))
```
Which methods must an iterator implement, and what ends the iteration?
An iterator's __next__ has run out of items. How does it signal "no more"?
What does this print?
```
def ab():
yield from ['a', 'b']
yield from ['c']
print(list(ab()))
```
What does this print?
```
def g():
yield 1
return 99
yield 2
print(list(g()))
```
What does this print?
```
import itertools
c = itertools.count(10, 2)
out = list(itertools.islice(c, 3))
print(out)
```
What does this print?
```
from itertools import groupby
data = [1, 1, 2, 1, 1]
out = [k for k, _ in groupby(data)]
print(out)
```
What does this print?
```
from itertools import groupby
g = list(groupby('AAB'))
print([list(grp) for k, grp in g])
```
What does this print?
```
from itertools import combinations
print(list(combinations('ABC', 2)))
```
What does this print?
```
from itertools import permutations
print(len(list(permutations('ABC', 2))))
```
What does this print?
```
from itertools import product
out = list(product('AB', repeat=2))
print(out)
```
What does this print?
```
from itertools import accumulate
print(list(accumulate([1, 2, 3, 4])))
```
What does this print?
```
from itertools import cycle, islice
c = cycle([1, 2, 3])
print(list(islice(c, 5)))
```
What does this print?
```
from itertools import chain
print(list(chain('AB', 'CD')))
```
What does this print?
```
z = zip([1, 2], ['a', 'b'])
print(list(z))
print(list(z))
```
What does this print?
```
z = zip([1, 2, 3], ['a', 'b'])
print(list(z))
```
What does this print?
```
l = [1, 2, 3]
print(iter(l) is iter(l))
a = iter(l)
print(a is iter(a))
```
What does this print?
```
import itertools as it
print(list(it.islice('ABCDEFG', 1, 5, 2)))
```
What does the two-argument form iter(callable, sentinel) do? (e.g. iter(f.readline, ''))
What does this print?
from itertools import compress
print(list(compress('ABCDEF', [1, 0, 1, 0, 1, 1])))
What does this print?
from itertools import dropwhile, takewhile
data = [1, 2, 3, 1, 2]
pred = lambda x: x < 3
print(list(takewhile(pred, data)))
print(list(dropwhile(pred, data)))
What does this print?
from itertools import filterfalse
print(list(filterfalse(lambda x: x % 2, [1, 2, 3, 4, 5])))
What does this print?
def g():
x = 10
yield x
x += 5
yield x
it = g()
print(next(it))
print(next(it))
What does this print?
def sub():
yield 1
return 99
def main():
result = yield from sub()
print('sub returned', result)
print(list(main()))
When may you drop the parentheses of a generator expression, as in sum(i*i for i in range(1, 11))?
What does this print, and why is defining iteration this way convenient?
class Countdown:
def __init__(self, n):
self.n = n
def __iter__(self):
for i in range(self.n, 0, -1):
yield i
print(list(Countdown(3)))
What does this print?
class Count:
def __init__(self, n):
self.n, self.i = n, 0
def __iter__(self):
return self
def __next__(self):
if self.i >= self.n:
raise StopIteration
self.i += 1
return self.i
c = Count(2)
for a in c:
for b in c:
print(a, b)
What is the difference between repeat(obj, times) and repeat(obj) in itertools?
What does this print?
from itertools import starmap
print(list(starmap(pow, [(2, 3), (2, 4), (3, 2)])))
What does itertools.tee(iterable, n) give you, and what must you avoid doing afterward?
What does this print, and what one-line mistake would break it?
def printer():
while True:
value = (yield)
print('got', value)
p = printer()
next(p)
p.send('hi')
What does a generator's throw(exc) method do, and where does the exception surface?
In a consuming generator, one of these lines does NOT do what it looks like. Which, and why?
a = yield # (1)
a = yield 5 # (2)
a = (yield) + 10 # (3)
a = yield + 10 # (4)
How can a class be made iterable without defining __iter__ or __next__ at all?
Start learning today
Free to start — download the app or use it in your browser.
