×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# All combinations of a list of boolean items2

## 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

[[('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

2
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)]] 

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

(OP)
Good morning mikrom,

That is exactly the thing!

Thanks.

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

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)]] 

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

(OP)
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.

#### CODE --> 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]) 

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!