Assume I have n variables that each take on two values: 0 or 1. If I wanted to enumerate all possible combinations of values, that would be 2^n possible combinations. I was wondering how to generate this in a clean and simple way?
Imagine n=4. We would want to produce a numpy array or something similar like the following manually generated example.
[[0 0 0 0]
[0 0 0 1]
[0 0 1 0]
[0 0 1 1]
[0 1 0 0]
[0 1 0 1]
[0 1 1 0]
[0 1 1 1]
[1 0 0 0]
[1 0 0 1]
[1 0 1 0]
[1 0 1 1]
[1 1 0 0]
[1 1 0 1]
[1 1 1 0]
[1 1 1 1]]
Note that the ordering matters. The first column always looks at cases for col1 = 0, then moves on to cases where col1 = 1. Then col2 looks at cases where col2 = 0 given that col1 = 0, then col2 = 1 given that col1 = 0, then col2 = 0 given that col1 = 1, and finally col2 = 1 given that col1 = 1. And so on. Basically I would need this kind of ordering approach to hold regardless on n.
Can this be solved through an iterative approach?