Itertools islice makes an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. If stop is None, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position
from itertools import islice
numbers = range(10)
start = 2
stop = 8
step = 2
print list(islice(numbers, stop))
print list(islice(numbers, start, stop))
print list(islice(numbers, start, stop, step))
[0, 1, 2, 3, 4, 5, 6, 7]
[2, 3, 4, 5, 6, 7]
[2, 4, 6]
No comments:
Post a Comment