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')]
Related posts
- Some more python recursion examples — posted 2011-10-05
- Free Computer Science courses online — posted 2009-06-30
- Find the N longest lines in a file with Python — posted 2009-06-28
- How to reverse words in a sentence using Python and C — posted 2009-04-22
- Python recursion example to navigate tree data — posted 2008-08-19