I've been experimenting with a number of techniques but I'm sure there's smooth-ish way to get this done.
Suppose I have two lists with the same amount of items in them (4 each):
a = ['a', 'b', 'c', 'd']
b = [1, 2, 3, 4]
I'd like to merge these lists in all possible ways while retaining order. Example outputs:
a, b, c, d, 1, 2, 3, 4
1, 2, 3, 4, a, b, c, d
a, b, 1, 2, c, 3, 4, d
The point is each of the lists must retain its order so an item can not precede another item in the output considering its position in the list. so for example the output can not be:
a, b, **d**, c, 1... > d precedes c whereas c is before d in the original list
1, **4**, a, b, 3.... > 4 precedes 3 whereas 3 is before 4 in the original list
I guess the idea is to merge the second list into the first list in all possible ways. A fully worked example is this:
a = [a, b]
b = [1, 2]
desired output:
ab12
a1b2
a12b
1ab2
1a2b
12ab
How do I go about doing this? Does itertools
have a capability to do this in some way? Or is there another way to get this done? Please help!