(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 the same length:
a = [1,2,3]
b = [4,5,6]
And I want to obtain all possible replacements of the first list with the second list.
output[0] = [1,2,3] # no replacements
output[1] = [4,2,3] # first item was replaced
output[2] = [1,5,3] # second item was replaced
output[3] = [1,2,6] # third item was replaced
output[4] = [4,5,3] # first and second items were replaced
output[5] = [4,2,6] # first and third items were replaced
output[6] = [1,5,6] # second and third items were replaced
output[7] = [4,5,6] # all items were replaced
Please note that this question is NOT answered by the following questions:
- How to get all possible combinations of a list’s elements?
- combinations between two lists?
- Python merging two lists with all possible permutations
- All combinations of a list of lists
A possible solution involving the previously linked answers would be to create several lists and then use the itertools.product method on them. For example, instead of having 2 lists of 3 elements, I could create 3 lists of 2 elements; however, that would over-complicate the code and I'd prefer to avoid that if I could.
Is there an easy and quick way to do it?