Align two lists by adding special values for missing entries
python
Solution
How about something like this:
set1 = set(list1)
set2 = set(list2)
total = sorted(set1|set2)
new_list1 = [x if x in set1 else "MISSING" for x in total]
new_list2 = [x if x in set2 else "MISSING" for x in total]
Problem
I have two lists of values of the same sortable type, which are sorted in ascending order, but (i) they don't have the same length, and (ii) entries present in one list can be missing from the other and vice versa. However I know that the majority of values in one list are present in the other, and that there are no duplicates in any list. So we may have this situation: ``` list1 = [value1-0, value1-1, value1-2, value1-3] list2 = [value2-0, value2-1, value2-2] ``` If it happens that the order of the values from both lists is: ``` value1-0 < (value1-1 = value2-0) < value2-1 < value1-2 < value1-3 < value2-2 ``` we can give combined sorted value names to the values across the two lists, e.g.: ``` valueA < valueB < valueC < valueD < valueE < valueF ``` so that the two lists can be written as: ``` list1 = [valueA, valueB, valueD, valueE] list2 = [valueB, valueC, valueF] ``` Given this I want the lists to become: ``` new_list1 = [valueA, valueB, "MISSING", valueD, valueE, "MISSING"] new_list2 = ["MISSING", valueB, valueC, "MISSING", "MISSING", valueF ] ``` Can anyone help? EDIT: The original question referred to `datetime` objects in particular (hence the comments specific to `datetime`s), but has been generalized to any sortable type.