Merge sorted streams without sorting again
Two sorted sources already contain most of the ordering information. Compare their next values and advance only the source chosen. Taking the left source on equality preserves its order relative to the right source. The same pattern supports merge sort, sorted file processing and joining ordered event streams.
left = [1, 4]
right = [2, 3]
print(left[0] <= right[0])Think before coding
Every comparison consumes one item, so merging takes linear time. The remaining suffix is already sorted after one side is exhausted. Choosing the left item on a tie makes the merge stable. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write merge_sorted(left, right), returning a new sorted list from two already-sorted lists. Preserve duplicates, do not call sorted or list.sort, and do not change the inputs.
The explanation opens here once every check passes.