Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

All combinations of a list of boolean items 2

Status
Not open for further replies.

mintjulep

Technical User
Aug 20, 2004
1,536
0
36
JP
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

[pre][[('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)]][/pre]

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)
 
Hi mintjulep,

maybe something like this:
Code:
>>> import itertools

>>> my_list = ['a', 'b', 'c']

>>> [list(zip(my_list, x)) for x in itertools.product([True, False], repeat=len(my_list))]

[[('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)]]
 
Good morning mikrom,

That is exactly the thing!

Thanks.
 
Hi mintjulep,
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:
my_list = ['a', 'b']
which length is 2, it generates this list of four 2-tuples:
Code:
>>> [x for x in itertools.product([True, False], repeat=len(my_list))]
[(True, True), 
 (True, False), 
 (False, True), 
 (False, False)]

Then I used the function zip() to join my_list with every element of the list generated by itertools.product(), e.g.:
Code:
>>> list(zip(my_list, (False, True)))
[('a', False), ('b', True)]

All together it gives the result:
Code:
>>> [list(zip(my_list, x)) for x in itertools.product([True, False], repeat=len(my_list))]
[[('a', True), ('b', True)], 
 [('a', True), ('b', False)], 
 [('a', False), ('b', True)], 
 [('a', False), ('b', False)]]
 
Thank you for taking the time to explain it.

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

Python:
import itertools
my_list = ['a', 'b', 'c', 'd']
answer = [list(zip(my_list, x)) for x in itertools.product([True, False], repeat=len(my_list))]
dlist = [dict(h) for h in answer] 
for count in range(len(dlist)):
    print (dlist[count])
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top