Home Iterating over combinatorics
Post
Cancel

Iterating over combinatorics

Very often, there is a need for implementing combinatorics (permutations, combinations and variations) and iterating over the generated values in a list for example. While it is possible to create user-tailored solution to it, there is module in Python that provides solutions to those problems.

The itertools module provides four methods for implementing combinatorics:

  • product()
  • itertools.permutations()
  • itertools.combinations()
  • itertools.combinations_with_replacement()

Examples:

1
product('ABCD', repeat=2)

Output:

1
AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
1
permutations('ABCD', 2)

Output:

1
AB AC AD BA BC BD CA CB CD DA DB DC
1
combinations('ABCD', 2)

Output:

1
AB AC AD BC BD CD
1
combinations_with_replacement('ABCD', 2)

Output:

1
AA AB AC AD BB BC BD CC CD DD

The user can choose one of those four methods depending on the type of combinatorics required. Basically, the product() method can be configured to output variations with repetition – basically all possible ways to combine a given set of data. The purpose of other three methods is self-explanatory. The first method itertools.permutations() takes a list or another collection and produces a sequence of tuples which generate all permutations’ possibilities. For example:

1
2
3
4
collection=['a','b','c']
from itertools import permutations
for x in permutations(collection):
    print(x)

Output:

1
2
3
4
5
6
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')

As demonstrated in the first example, it is possible to set a length of an output data:

1
2
for x in permutations(collection,2):
    print(x)

Output:

1
2
3
4
5
6
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')

Going further, itertools.combinations() produce a sequence of combinations from the input items. For example:

1
2
3
# (using the code from the previous example)
for x in combinations(collection,3):
    print(x)

Output:

1
('a', 'b', 'c')

Those are combinations without repetition (i.e. repetitive characters are not produced as they are considered the same). Combinations with repetition can be generated as follows:

1
2
3
from itertools import combinations_with_replacement
for x in combinations_with_replacement(collection,3):
    print(x)

Output:

1
2
3
4
5
6
7
8
9
10
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')

The itertools module contains other interesting tools, like infinite iterators and terminating iterators. Certainly, when it comes to combinatorics and aforementioned other topics, it is the quickest way of implementing solutions to those problems.

This post is licensed under CC BY 4.0 by the author.