itertools
is a standard Python module that provides fast, memory-efficient tools for working with iterators. It includes functions for creating infinite iterators, chaining iterators, filtering, grouping, and combining elements.
import itertools
# 1. count(): infinite counting
for i in itertools.count(5):
if i > 8:
break
print(i)
# Output: 5 6 7 8
# 2. cycle(): repeat elements endlessly
count = 0
for item in itertools.cycle(["A", "B"]):
if count > 3:
break
print(item)
count += 1
# Output: A B A B
# 3. repeat(): repeat an object multiple times
for item in itertools.repeat("Hello", 3):
print(item)
# Output: Hello Hello Hello
# 4. chain(): combine multiple iterables
a = [1, 2]
b = [3, 4]
for x in itertools.chain(a, b):
print(x)
# Output: 1 2 3 4
# 5. combinations(): all possible pairs
items = [1, 2, 3]
for combo in itertools.combinations(items, 2):
print(combo)
# Output: (1, 2) (1, 3) (2, 3)
# 6. permutations(): all possible orders
for p in itertools.permutations([1, 2], 2):
print(p)
# Output: (1, 2) (2, 1)
# 7. product(): cartesian product
for prod in itertools.product([1, 2], ['a', 'b']):
print(prod)
# Output: (1, 'a') (1, 'b') (2, 'a') (2, 'b')
# 8. groupby(): group by condition or key
data = [("A", 1), ("A", 2), ("B", 3)]
for key, group in itertools.groupby(data, key=lambda x: x[0]):
print(key, list(group))
# Output: A [('A', 1), ('A', 2)] B [('B', 3)]