Given two lists e.g.
l1 = ["A","D","B","C"]
l2 = ["X","A","Y","B"]
I want the result to preserve the order given by both input lists i.e.
["X","A","D","Y","B","C"]
The result is not unique as "D" and "Y" could also be switched (but if it is not clear lexicographic order should handle the conflict) Also if I had smth. like
l1 = ["B","A"]
l2 = ["X","A","Y","B"]
positions of either B or A should be treated interchangeably (i.e. no unique order can be constructed and the order of B< A and B> A should be treated as A=B) which would lead to accepted results
["X","A","Y","B"]
(preferably as it is in deterministic lexicographic order of A and B) or
["X","B","Y","A"]
Put in another way I want the combination of lists to preserve the order where unique and where it's not, the result should be deterministic e.g. according to lexicographic ordering.
Is there a library for python that accomplishes that or would I have to implement it on my own? I looked at OrderedDict and OrderedSet but both do not handle the merge as I want.