I'd like to merge to lists with many similar elements preserving the order as much as possible.
So say for example we have this:
list1=[1,2,3,4,6,7,8,9]
list2=[1,2,3,4,5,8,10]
list1 list2=[1,2,3,4,6,5,7,8,9,10]
or maybe:
list1 list2=[1,2,3,4,5,6,7,8,10,9]
The idea is that 1,2,3,4 all match. Then we encounter 5 in list1 and 6 in list2 and we insert them next to each other in the merge list. If there are more elements in one list than the other then the extra rows are just appended to the end.
CodePudding user response:
- For unsorted output-
list(set(list1 list2))
- For sorted output-
list(sorted(set(list1 list2)))
CodePudding user response:
list1=[1,2,3,4,6,7,8,9]
list2=[1,2,3,4,5,8,10]
new_list = list(set(list1 list2)).sort()