Python List Comprehensions and Generators Flashcards
6 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Python List Comprehensions and Generators flashcards as text
What is the purpose of `itertools.chain()`?
Answer: Chains multiple iterables together into one continuous iterator
itertools.chain() takes multiple iterables and yields elements from each one in sequence.
What does `enumerate(['a','b','c'])` produce?
Answer: [(0,'a'),(1,'b'),(2,'c')]
enumerate() wraps an iterable and yields (index, value) tuples starting at 0 by default.
Which statement about generator objects is TRUE?
Answer: They are single-use; once exhausted they cannot be reset
A generator maintains internal state and once exhausted it raises StopIteration on any further next() call; it cannot be rewound.
What is the output of `sum(x for x in range(1, 6))`?
Answer: 15
range(1,6) is 1,2,3,4,5; their sum is 15.
What does `list(zip([1,2,3], ['a','b','c']))` return?
Answer: [(1,'a'),(2,'b'),(3,'c')]
zip() pairs up elements at the same position from each iterable into tuples.
What is the result of `[str(x) for x in range(3)]`?
Answer: ['0', '1', '2']
str() converts each integer to its string representation, producing ['0', '1', '2'].