Python Programming - Generators - Discussion
Discussion Forum : Generators - General Questions (Q.No. 63)
63.
What is the purpose of the
itertools.groupby()
function when used with generators?
Answer: Option
Explanation:
itertools.groupby()
groups consecutive equal elements from the generator based on a key function.
import itertools
def my_generator():
yield 1
yield 1
yield 2
yield 3
yield 3
grouped_generator = itertools.groupby(my_generator())
for key, group in grouped_generator:
print(f"Key: {key}, Group: {list(group)}")
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