Two issues:
It's not stable, for example mergesort([1, 1.0]) returns [1.0, 1]. Stability is desirable, and you could easily achieve it by using <= instead of <.
If the given list has more than one element, you return a new list. But if not, then you return the given list. That's inconsistent and dangerous, might trick someone into thinking you return a separate list, and then getting a problem when you don't.
And it's a simple algorithm, but I can barely see it because of the long and unusual variable names. I feel like I'm looking at a wall of characters. I'd use standard single letter names:
def merge(A, B): merged = [] i = j = 0 while i < len(A) and j < len(B): if A[i] <= B[j]: merged.append(A[i]) i += 1 else: merged.append(B[j]) j += 1 merged += A[i:] or B[j:] return merged
(I also shortened the part after the loop.)
And here's another way to make it faster, using a for loop for one of the inputs:
def merge(A, B): merged = [] j = 0 for a in A: while j < len(B) and B[j] < a: merged.append(B[j]) j += 1 merged.append(a) merged += B[j:] return merged
Or instead of j < len(B), we can make sure the last element comes from B (this modifies the merge inputs and assumes they're not empty, which is fine within the context of the mergesort function):
def merge(A, B): merged = [] if B[-1] < A[-1]: B.append(A.pop()) j = 0 for a in A: while B[j] < a: merged.append(B[j]) j += 1 merged.append(a) merged += B[j:] return merged
Times for sorting 10000 random numbers using our various merge functions:
31.7 ± 0.5 ms merge_yours 31.8 ± 0.4 ms merge_my_first 23.0 ± 0.5 ms merge_my_second 17.3 ± 0.3 ms merge_my_third
Full code hidden in HTML comment here.
Attempt This Online!