(I am aware that the title of the question might be misleading, but I could not find any other way to formulate it - feel free to edit it)
I have two lists, both of
Each item may independently be replaced or left alone. This can be modeled by a bit being 1 or 0. If you consider each item to be a separate bit, then iterating over all of the possibilities can be mapped to iterating over all of the combinations of n bits.
In other words, iterate from 0 to 2n-1 and look at the bit patterns.
n = len(a)
for i in range(2**n):
yield [a[j] if i & (1 << j) != 0 else b[j] for j in range(n)]
Breaking this down, i & (1 << j) != 0 checks if the jth bit of i is set. If it is, use a[j], otherwise b[j].
Result:
[1, 2, 3]
[4, 2, 3]
[1, 5, 3]
[4, 5, 3]
[1, 2, 6]
[4, 2, 6]
[1, 5, 6]
[4, 5, 6]