A combinatoric iterator will perform a set of statistical or mathematical operations on an input iterable.
A useful itertool that is a combinatoric iterator is the
combinations() itertool. This itertool will produce an iterator of tuples that contain combinations of all elements in the input.
combinations() itertool takes in two inputs, the first is an iterable, and the second is a value
r that represents the length of each combination tuple.
The return type of
combinations() is an iterator that can be used in a
for loop or can be converted into an iterable type using
list() or a
To show how it’s used, suppose we have a list of even numbers and we want all possible combinations of 2 even numbers:
import itertools even = [2, 4, 6] even_combinations = list(itertools.combinations(even, 2)) print(even_combinations)
- Import the module
- Create an iterator using
listof even numbers as the first argument and
2as the second argument.
even_combinationsequal to a
listof the elements in the iterator returned from
even_combinations. The resulting list of
2member tuples are the combinations of all 3 members of
even:[(2, 4), (2, 6), (4, 6)]
Now, let’s try using a combinations itertool within our pet store!
We have another shelving unit to display by the register that can only hold 3 collars. We have a list of collars of varying colors and sizes.
We want to know how many different combinations exist to display a set of 3 collars. Use the
combinations() itertool to do this. Set the returned iterator to a variable named
for loop, print each item in
collar_combo_iterator to see all the possible collar combinations.