Python Programming - Generators - Discussion
Discussion Forum : Generators - General Questions (Q.No. 79)
79.
What is the purpose of the
itertools.takewhile()
function when used with generators?
Answer: Option
Explanation:
itertools.takewhile()
yields elements from the generator as long as the specified condition is true, stopping when the condition becomes false.
import itertools
def my_generator():
for i in range(10):
yield i
filtered_iterable = itertools.takewhile(lambda x: x < 5, my_generator())
print(list(filtered_iterable)) # Output: [0, 1, 2, 3, 4]
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers