[Solved] making a code that can fill in automatically a list with restrictions


If you want all combinations of [0, 1] for a length of 7 then you can take the cartesian product of [0, 1] repeated 7 times, e.g.:

In []:
import itertools as it
list(it.product([0, 1], repeat=7))

Out[]:
[(0, 0, 0, 0, 0, 0, 0),
 (0, 0, 0, 0, 0, 0, 1),
 (0, 0, 0, 0, 0, 1, 0),
 (0, 0, 0, 0, 0, 1, 1),
 (0, 0, 0, 0, 1, 0, 0),
 ...

You don’t need to create independent variables for each item as you can just index them in the list:

In []:
vars = list(it.product([0, 1], repeat=7))
vars[0]

Out[]:
(0, 0, 0, 0, 0, 0, 0)

If you do really need them named, then a dict would be the appropriate structure:

In []:
vars = {'List{}'.format(i): v for i, v in enumerate(it.product([0, 1], repeat=7), 1)}
vars['List128']

Out[]:
(1, 1, 1, 1, 1, 1, 1)

Because you are just wanting a binary form of the number the alternative is just to convert the number to a binary string, e.g.:

In []:
[tuple(map(int, format(i, '07b'))) for i in range(128)]

Out[]:
[(0, 0, 0, 0, 0, 0, 0),
 (0, 0, 0, 0, 0, 0, 1),
 (0, 0, 0, 0, 0, 1, 0),
 (0, 0, 0, 0, 0, 1, 1),
 (0, 0, 0, 0, 1, 0, 0),
 ...

0

solved making a code that can fill in automatically a list with restrictions