## All combinations of a list of boolean items

## All combinations of a list of boolean items

(OP)

Consider a list of n items = ['a', 'b', 'c', ... n]

Each item can take a boolean state, i.e. True or False. Represented as a list [True, False]

How can I generate a list of lists of all the combinations of states.

For the example of

items = ['a', 'b', 'c']

states = [True, False]

The desired output would be

Brute force looping could be done, but it seems there must be some Pythonic comprehension, possibly involving itertools. But I can't figure one out (because I'm not very good at Python yet)

Each item can take a boolean state, i.e. True or False. Represented as a list [True, False]

How can I generate a list of lists of all the combinations of states.

For the example of

items = ['a', 'b', 'c']

states = [True, False]

The desired output would be

[[('a', True), ('b', True), ('c', True)], [('a', True), ('b', True), ('c', False)], [('a', True), ('b', False), ('c', True)], [('a', True), ('b', False), ('c', False)], [('a', False), ('b', True), ('c', True)], [('a', False), ('b', True), ('c', False)], [('a', False), ('b', False), ('c', True)], [('a', False), ('b', False), ('c', False)]]

Brute force looping could be done, but it seems there must be some Pythonic comprehension, possibly involving itertools. But I can't figure one out (because I'm not very good at Python yet)

## RE: All combinations of a list of boolean items

maybe something like this:

## CODE

## RE: All combinations of a list of boolean items

That is exactly the thing!

Thanks.

## RE: All combinations of a list of boolean items

I'm glad it's useful for you.

Maybe a little explanation how it works:

I used

itertools.product()which generates list of all n-tuples of 2 elements (True, False), where n is length of the given list.For example for a given list

## CODE

## CODE

Then I used the function

zip()to join my_list with every element of the list generated by itertools.product(), e.g.:## CODE

All together it gives the result:

## CODE

## RE: All combinations of a list of boolean items

I've realized that what I really need is a dictionary of each combination, so I've tacked on.

## CODE --> Python