Search code examples
pythonpython-itertools

Creating a matrix of options using itertools


I am trying to produce a matrix of True and False values, which shows all the permutations for a given number of choices. So for 5 choices you would have the following output.

F F F F F
T F F F F
T T F F F
T T T F F
...
F T F F F
...

I have been looking at using itertool's permutations and combinations, but these work off position and not value which results in duplicates.

I'm sure there is a standard algorithm for this problem, but I'm struggling to find its name.


Solution

  • Use itertools.product:

    itertools.product([False,True],repeat=5)
    

    example of itertools.product([False,True],repeat=2):

    (False, False)
    (False, True)
    (True, False)
    (True, True)