← Todos los cuestionarios públicos
Empezar el cuestionario (como invitado o con cuenta)
Python — Comprehensions, iterators & generators
por Jude · 23/05/2026 · 124 vistas · compartidos
Comprehensions (list/dict/set), iterator protocol,
yield, generator pipelines and itertools. 20 questions.
20
Preguntas
🇬🇧 English
Idioma
Difícil
Dificultad
Las preguntas del cuestionario
-
1. What does
[x * x for x in range(5) if x % 2]evaluate to?- [0, 1, 4, 9, 16]
- [1, 9]
- [1, 4, 9]
- [1, 9, 25]
-
2. Difference between
(x for x in r)and[x for x in r]?- None
- First is a generator (lazy); second is a list (eager)
- First returns a tuple
- First is faster but uses more memory
-
3. What protocol must an iterator implement?
__iter__and__next____getitem__only__call____index__
-
4. What does this print?
def gen(): yield 1 yield 2 yield 3 print(sum(gen()))- 3
- 6
- [1, 2, 3]
- TypeError
-
5. What is the output?
def gen(): x = yield 1 yield x + 10 g = gen() print(next(g)) print(g.send(5))- 1 then 15
- 1 then 11
- 5 then 15
- StopIteration
-
6. Which
itertoolsfunction chains iterables together?chainzip_longestproducttee
-
7. What does this print?
g = (i*i for i in range(3)) print(list(g)) print(list(g))- [0, 1, 4] then [0, 1, 4]
- [0, 1, 4] then []
- [] then []
- Error
-
8. How do you delegate to another iterable in a generator?
yield from iterableyield *iterablereturn iterableraise iterable
-
9. Complexity of
any(x < 0 for x in big_list)?- Always O(n)
- Best O(1) if early match, worst O(n) — short-circuits
- O(n log n)
- O(1) memory, O(0) time
-
10. What does this print?
d = {k: v for k, v in [('a',1),('b',2),('a',3)]} print(d)- {'a': 1, 'b': 2}
- {'a': 3, 'b': 2}
- {'a': [1, 3], 'b': 2}
- Error
-
11. What does
list(zip([1,2,3], ['a','b']))return?- [(1, 'a'), (2, 'b')]
- [(1, 'a'), (2, 'b'), (3, None)]
- Error
- [1, 2, 3, 'a', 'b']
-
12. What does this produce?
from itertools import islice, count for x in islice(count(10, 2), 4): print(x, end=' ')- 10 11 12 13
- 10 12 14 16
- 10 12 14
- Error
-
13. How do you implement a lazy chunks of n generator?
for c in chunks: yield list(c)withitertools.islicein a loop until emptymap(chunked, lst)list(zip(lst, lst, lst))- Impossible without numpy
-
14. What is the output?
def g(): try: yield 1 finally: print("end") for x in g(): print(x) break- 1
- 1 then end
- end then 1
- Generator does not call finally on break
-
15. Which expression is a set comprehension?
{x for x in r}{x: 0 for x in r}(x for x in r)[x for x in r]
-
16. What does
enumerate(seq, start=10)yield forseq=['a','b']?- (0, 'a'), (1, 'b')
- (10, 'a'), (11, 'b')
- (1, 'a'), (2, 'b')
- Error
-
17. What is the output?
from itertools import groupby data = sorted("AaBbAa", key=str.lower) result = [(k, len(list(g))) for k, g in groupby(data, key=str.lower)] print(result)- [('a', 4), ('b', 2)]
- [('A', 2), ('a', 2), ('B', 1), ('b', 1)]
- [('a', 6)]
- Error
-
18. What is special about
itertools.tee?- Splits one iterator into n independent iterators (with memory cost if they advance at different rates)
- Reverses an iterator
- Joins two iterators
- Filters an iterator
-
19. What's the result of
next(iter([]), 'def')?- StopIteration
- None
- 'def'
- Error
-
20. Which generator pattern correctly streams a large file by line and uppercases each line lazily?
(line.upper() for line in open(p))open(p).read().upper().splitlines()[l.upper() for l in open(p)]map(str.upper, open(p).readlines())