Python Programming - Generators

Exercise : Generators - General Questions
  • Generators - General Questions
86.
How does the itertools.compress() function differ from itertools.filterfalse() when used with generators?
They are equivalent in functionality
itertools.compress() filters elements based on a true condition, while itertools.filterfalse() filters based on a false condition
itertools.filterfalse() filters elements based on a true condition, while itertools.compress() filters based on a false condition
itertools.compress() raises a ValueError if the selectors iterable is shorter than the data iterable
Answer: Option
Explanation:
itertools.compress() filters elements from the data iterable based on corresponding truth values in the selectors iterable. itertools.filterfalse() filters elements based on a false condition.

87.
How does the generator.throw(StopIteration) method differ from raise StopIteration when used with a generator?
They have the same effect on the generator
generator.throw(StopIteration) stops the generator and raises a StopIteration exception, while raise StopIteration is used outside the generator
raise StopIteration stops the generator and raises a StopIteration exception, while generator.throw(StopIteration) is used outside the generator
generator.throw(StopIteration) raises a ValueError
Answer: Option
Explanation:
generator.throw(StopIteration) is used to raise a StopIteration exception inside the generator, while raise StopIteration is used outside the generator to signal the end of iteration.

88.
How does the itertools.zip_longest() function differ from zip() when used with generators?
They are equivalent in functionality
itertools.zip_longest() stops when the longest iterable is exhausted, while zip() stops when the shortest iterable is exhausted
zip() raises a ValueError when used with generators
itertools.zip_longest() raises a StopIteration exception when used with generators
Answer: Option
Explanation:
itertools.zip_longest() continues until the longest iterable is exhausted, filling in missing values with a specified fillvalue. zip() stops when the shortest iterable is exhausted.
import itertools

gen1 = (1, 2, 3)
gen2 = ('a', 'b')

zipped_iterable = itertools.zip_longest(gen1, gen2, fillvalue=None)
regular_zip = zip(gen1, gen2)

print(list(zipped_iterable))    # Output: [(1, 'a'), (2, 'b'), (3, None)]
print(list(regular_zip))         # Output: [(1, 'a'), (2, 'b')]

89.
How does the itertools.islice() function differ from using slice() with a generator?
They are equivalent in functionality
itertools.islice() allows for arbitrary slicing of a generator, while slice() is used for indexing
slice() allows for arbitrary slicing of a generator, while itertools.islice() is used for indexing
itertools.islice() and slice() both raise a ValueError
Answer: Option
Explanation:
itertools.islice() is specifically designed for slicing iterables, including generators. slice() is used for indexing sequences, not for creating a sliced iterable.

90.
What is the purpose of the itertools.accumulate() function when used with generators?
It concatenates multiple generators
It applies a specified function cumulatively to the elements of a generator
It stops the generator after one iteration
It interleaves values from different generators
Answer: Option
Explanation:
itertools.accumulate() applies a specified function cumulatively to the elements of a generator, yielding the intermediate results.
import itertools

gen = (1, 2, 3, 4)
accumulated_iterable = itertools.accumulate(gen, lambda x, y: x + y)

print(list(accumulated_iterable))  # Output: [1, 3, 6, 10]