01Count and islice
main.py
from itertools import count, islice
print(list(islice(count(10), 5)))Output
[10, 11, 12, 13, 14]
Python itertools module: count, cycle, islice, chain, combinations, and permutations for efficient iterator building blocks.
from itertools import count, islice
print(list(islice(count(10), 5)))[10, 11, 12, 13, 14]
from itertools import cycle, islice
colors = cycle(["red", "green"])
print(list(islice(colors, 4)))['red', 'green', 'red', 'green']
from itertools import chain
print(list(chain([1, 2], [3, 4], [5])))[1, 2, 3, 4, 5]
from itertools import combinations
print(list(combinations([1, 2, 3], 2)))[(1, 2), (1, 3), (2, 3)]
from itertools import permutations
print(list(permutations([1, 2, 3], 2)))[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]