# Get All Possible Boolean Sequences For A Given List Length

## 23 March 2022 - 1 answer

Given a random list length, how can I efficiently get all possible sequences of boolean values, except strings that are all True or all False?

For instance, given the number 3 it should return something like the following.

``````[True, False, False],
[True, True, False],
[True, False, True],
[False, True, False],
[False, True, True],
[False, False, True],
``````

Is there already a known function that does this?

The order that it returns the sequences in is not important. I mainly just need a number of how many sequences are possible for a given list length.

This is mostly a maths question, unless you need the sequences themselves. If you do, there is a neat python solution:

``````from itertools import product

[seq for seq in product((True, False), repeat=3)][1:-1]
``````

The list comprehension will contain all possible sequences, but we don't want `(True, True, True)` and `(False, False, False)`. Conveniently, these will be the first and last element respectively, so we can simply discard them, using slicing from 1 to -1.

For sequences with different lengths, just change the "repeat" optional argument of the `itertools.product` function.