SaltyCrane Blog — Notes on JavaScript and web development

Find all combinations of a set of lists with itertools.product

Copied from http://stackoverflow.com/questions/2853212/all-possible-permutations-of-a-set-of-lists-in-python. Documentation: itertools.product

import itertools
from pprint import pprint

inputdata = [
    ['a', 'b', 'c'],
    ['d'],
    ['e', 'f'],
]
result = list(itertools.product(*inputdata))
pprint(result)

Results:

[('a', 'd', 'e'),
 ('a', 'd', 'f'),
 ('b', 'd', 'e'),
 ('b', 'd', 'f'),
 ('c', 'd', 'e'),
 ('c', 'd', 'f')]

Comments