Python Itertools
The itertools module is a part of Python’s standard library and gives us a collection of fast, memory-efficient tools to create and manipulate iterators. It is extremely helpful for tasks involving combinatoric and iterative processes. Let’s dive into the features of this module:
Key Features of itertools
- Infinite Iterators
These iterators generate sequences indefinitely and are useful for generating data lazily (on-demand).
2. Finite Iterators
These provide tools for working with finite data collections in an efficient manner.
3. Combinatoric Iterators
These iterators simplify tasks related to permutations, combinations, and Cartesian products.
1. Infinite Iterators
a) count(start=0, step=1)
- Generates an infinite sequence of numbers, starting at
start
and incremented bystep
.
import itertools
for number in itertools.count(10, 2): # Starts at 10, increments by 2
if number > 20:
break
print(number) # Outputs: 10, 12, 14, 16, 18, 20
b) cycle(iterable)
- Cycles through the elements of an iterable indefinitely.
import itertools
counter = 0
for item in itertools.cycle(['A', 'B', 'C']):
print(item) # Outputs: A, B, C, A, B, C, ...
counter += 1
if counter == 6:
break
c) repeat(object, times=None)
- Repeats the given object infinitely or for a specified number of times.
import itertools
print(list(itertools.repeat(5, 4))) # Outputs: [5, 5, 5, 5]
2. Finite Iterators
a) accumulate(iterable, func=operator.add)
- Returns accumulated sums (or other binary operations) of elements in the iterable.
import itertools
import operator
nums = [1, 2, 3, 4]
print(list(itertools.accumulate(nums))) # Outputs: [1, 3, 6, 10]
print(list(itertools.accumulate(nums, operator.mul))) # Outputs: [1, 2, 6, 24]
b) chain(*iterables)
- Chains multiple iterables into a single sequence.
import itertools
print(list(itertools.chain('ABC', 'DEF'))) # Outputs: ['A', 'B', 'C', 'D', 'E', 'F']
c) compress(data, selectors)
- Filters elements of
data
based onselectors
.
import itertools
data = 'ABCDE'
selectors = [1, 0, 1, 0, 1]
print(list(itertools.compress(data, selectors))) # Outputs: ['A', 'C', 'E']
d. dropwhile(predicate, iterable)
- Drops elements from an iterable as long as the predicate is true; resumes yielding elements once it becomes false.
import itertools
nums = [1, 2, 3, 4, 5]
print(list(itertools.dropwhile(lambda x: x < 3, nums))) # Outputs: [3, 4, 5]
e) takewhile(predicate, iterable)
- Returns elements as long as the predicate is true; stops on the first false value.
import itertools
nums = [1, 2, 3, 4, 5]
print(list(itertools.takewhile(lambda x: x < 3, nums))) # Outputs: [1, 2]
f) groupby(iterable, key=None)
- Groups consecutive elements of an iterable based on a key function.
import itertools
data = 'AAAABBBCCDAA'
grouped = itertools.groupby(data)
for key, group in grouped:
print(key, list(group))
# Outputs:
# A ['A', 'A', 'A', 'A']
# B ['B', 'B', 'B']
# C ['C', 'C']
# D ['D']
# A ['A', 'A']
g) islice(iterable, start, stop, step=1)
- Returns selected elements from the iterable, similar to slicing.
import itertools
print(list(itertools.islice(range(10), 2, 8, 2))) # Outputs: [2, 4, 6]
3. Combinatoric Iterators
a) product(*iterables, repeat=1)
- Returns the Cartesian product of input iterables.
import itertools
print(list(itertools.product('AB', repeat=2)))
# Outputs: [('A', 'A'), ('A', 'B'), ('B', 'A'), ('B', 'B')]
b) permutations(iterable, r=None)
- Returns all possible orderings of elements (length
r
).
import itertools
print(list(itertools.permutations('ABC', 2)))
# Outputs: [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
c) combinations(iterable, r)
- Returns all possible combinations (order doesn’t matter) of
r
elements.
import itertools
print(list(itertools.combinations('ABC', 2)))
# Outputs: [('A', 'B'), ('A', 'C'), ('B', 'C')]
d) combinations_with_replacement(iterable, r)
- Returns combinations of elements allowing repetition.
import itertools
print(list(itertools.combinations_with_replacement('ABC', 2)))
# Outputs: [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
Use Cases of itertools
- Data Manipulation: Filter, group, or transform sequences.
- Combinatorics: Simplify work with permutations or combinations.
- Memory Efficiency: Handling large datasets by generating elements lazily.
- Algorithm Design: Implementing iterative algorithms concisely.