I'm reposting this question because I was told that there is a solution for that in the last post.
I have 2 lists:
list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]
I want to have the possible combinations from these 2 lists, meaning:
[["foo", "bar", "lorem"],
["foo", "bar", "loremX"],
["foo", "barX", "loremX"],
["fooX", "bar", "loremX"],
["fooX", "barX", "loremX"],
["foo", "barX", "lorem"],
["fooX", "barX", "lorem"],
["fooX", "bar", "lorem"],
["foo", "bar", "lorem"],
["foo", "bar", "loremY"],
["foo", "barY", "loremY"],
["fooY", "bar", "loremY"],
["fooY", "barY", "loremY"],
["foo", "barY", "lorem"],
["fooY", "barY", "lorem"],
["fooY", "bar", "lorem"]]
Hope I didn't miss any combination.
Kinda lost with this one.
It probably should be something with itertools.combinations_with_replacement
Thanks.
EDIT
First of all, thanks to @titusarmah99 for a great answer. I managed to take his second-and-very-simple solution and make it generic:
import itertools
list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]
list2new = [""] + list2
newList = [[list1[i]+list2new[j] for j in range(len(list2new))] for i in range(len(list1))]
for index in range(1, len(list2) + 1):
for c in itertools.product([0,index],repeat=len(list1)):
tmp = [newList[i][c[i]] for i in range(len(c))]
print(tmp)