Last Updated: December 6, 2025
29 quizzes
Complete the expression to get an iterator over the values of the dictionary user_data
value_iter = user_data.()Click an option to fill the blank:
Create an iterator from the list records so that it can be advanced with next() in a processing loop
record_iter = (records)Click an option to fill the blank:
Complete the expression to build a generator that produces squared values from the existing iterable sensor_readings
squared = (reading ** 2 for reading in )Click an option to fill the blank:
Which statement best describes an iterator in Python?
What does the __iter__ method of an iterable typically return?
In a custom iterator class, what should __next__ do when there are no more items to produce?
Which of the following is TRUE about generator functions?
What is a key advantage of using generator expressions instead of list comprehensions for large datasets?
Which itertools function creates an infinite sequence of evenly spaced numbers?
What does the yield statement do inside a function?
Which expression creates a generator that yields the first 100 even numbers starting from 0?
What does itertools.cycle(colors) do when colors is a finite list?
You have a custom class LogLines that implements __iter__ but not __next__. Which is TRUE?
Which statement about custom iterator classes is correct?
Order the steps to create a custom iterator class that yields squares from 0 up to a given limit and then use it in a loop.
Drag and drop to reorder, or use the arrows.
Order the steps to build a generator-based data pipeline that reads lines from a file and filters only error messages.
Drag and drop to reorder, or use the arrows.
What is the output of this code using a custom iterator?
1class CountUp:
2 def __init__(self, limit):
3 self.current = 0
4 self.limit = limit
5 def __iter__(self):
6 return self
7 def __next__(self):
8 if self.current >= self.limit:
9 raise StopIteration
10 value = self.current
11 self.current += 2
12 return value
13counter = CountUp(5)
14print(list(counter))What is the output of this generator-based Fibonacci code?
1def fib(n):
2 a, b = 0, 1
3 for _ in range(n):
4 yield a
5 a, b = b, a + b
6series = fib(5)
7print(list(series))What is the output when using a generator expression with sum?
1data = [1, 2, 3, 4]
2result = sum(x * x for x in data)
3print(result)What is the output of this itertools.islice usage?
1import itertools
2numbers = itertools.count(start=10, step=5)
3first_three = list(itertools.islice(numbers, 3))
4print(first_three)What is the output of this code mixing iter() and next()?
1items = ['a', 'b', 'c']
2iterator = iter(items)
3next(iterator)
4value = next(iterator)
5print(value)Find the bug in this custom iterator that should yield lines from a file one by one.
Click on the line(s) that contain the bug.
class LineReader: def __init__(self, path): self.file = open(path, 'r') def __iter__(self): return self.file def __next__(self): line = self.file.readline() if not line: self.file.close() raise StopIteration return line.strip()Find the bug in this generator intended to yield even numbers up to a limit.
Click on the line(s) that contain the bug.
def even_numbers(limit): number = 0 while number < limit: if number % 2 == 0: yield number else: continue number += 2Match each itertools function with its correct description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the iterator-related concept with its appropriate example.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the generator function to lazily read and parse integers from a text file, one per line.
def read_ints(file_path): with open(file_path) as source: for raw_line in source: text = raw_line.() # remove surrounding whitespace if not text: continue yield (text)Click an option to fill blank 1:
Complete the code to build a memory-efficient pipeline that sums only the positive values from a stream of readings.
def sum_positive(readings): positives = (value for value in readings if value > 0) return (positives)data = [10, -5, 3, -1]result = sum_positive(data)(result)Click an option to fill blank 1:
Click the line that starts iteration over the generator created by generator_expression.
Click on the line to select.
numbers = [1, 2, 3, 4]generator_expression = (n * 10 for n in numbers)first_value = next(generator_expression)remaining_values = list(generator_expression)Click the line where StopIteration will be raised during this manual iteration.
Click on the line to select.
items = iter([10, 20])print(next(items))print(next(items))print(next(items))