Python Programming - Generators - Discussion
Discussion Forum : Generators - General Questions (Q.No. 59)
59.
How does the
itertools.islice()
function differ from using generator[start:end]
slicing?
Answer: Option
Explanation:
itertools.islice()
can be used for any iterable, not just generators, while slicing with generator[start:end]
is specific to generators and creates a new generator.
import itertools
def my_generator():
for i in range(10):
yield i
# Using itertools.islice() for slicing
sliced_iterable = itertools.islice(my_generator(), 2, 7)
for value in sliced_iterable:
print(value)
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