furas

Python - merging two sorted lists (Stackoverflow)

Sep 20th, 2025 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # date: 2025.09.20
  2.  
  3. # [How can I efficiently merge two sorted lists in Python without using merge() or heapq? - Stack Overflow](https://stackoverflow.com/questions/79770326/how-can-i-efficiently-merge-two-sorted-lists-in-python-without-using-merge-or)
  4.  
  5.  
  6. def merge_1(a: list, b: list) -> list:
  7.     # to keep original lists because I will use `.pop()`
  8.     a = a.copy()
  9.     b = b.copy()
  10.  
  11.     result = []
  12.  
  13.     while True:
  14.  
  15.         if (not a) or (not b):  # if one is empty (or both are empty)
  16.             # if a:
  17.             #    result += a
  18.             # else:
  19.             #    result += b
  20.             result += a + b
  21.             break
  22.         else:
  23.             if a[0] <= b[0]:
  24.                 result.append(a.pop(0))
  25.             else:
  26.                 result.append(b.pop(0))
  27.  
  28.     return result
  29.  
  30.  
  31. def merge_1b(a: list, b: list) -> list:
  32.     # to keep original list because I will use `.pop()`
  33.     a = a.copy()
  34.     b = b.copy()
  35.  
  36.     result = []
  37.  
  38.     while a and b:
  39.         if a[0] <= b[0]:
  40.             result.append(a.pop(0))
  41.         else:
  42.             result.append(b.pop(0))
  43.  
  44.     # if one of them is still not empty (or both are empty)
  45.     if a or b:
  46.         # if a:
  47.         #    result += a
  48.         # else:
  49.         #    result += b
  50.         result += a + b
  51.  
  52.     # if a:
  53.     #    result += a
  54.     # elif b:
  55.     #    result += b
  56.     # # else: print('both empty')
  57.  
  58.     return result
  59.  
  60.  
  61. def merge_2(a: list, b: list) -> list:
  62.  
  63.     len_a = len(a)
  64.     len_b = len(b)
  65.  
  66.     index_a = 0
  67.     index_b = 0
  68.  
  69.     result = []
  70.  
  71.     while True:
  72.  
  73.         if (index_a >= len_a) or (
  74.             index_b >= len_b
  75.         ):  # if one is empty (or both are empty)
  76.             # if index_a < len(a):
  77.             #    result += a[index_a:]
  78.             # else:
  79.             #    result += b[index_b:]
  80.             result += a[index_a:] + b[index_b:]
  81.             break
  82.         else:
  83.             if a[index_a] <= b[index_b]:
  84.                 result.append(a[index_a])
  85.                 index_a += 1
  86.             else:
  87.                 result.append(b[index_b])
  88.                 index_b += 1
  89.  
  90.     return result
  91.  
  92.  
  93. def merge_recursion(a: list, b: list) -> list:
  94.     # if (not a) and (not b):  # if both are empty
  95.     #     return []
  96.  
  97.     # if not a:
  98.     #     return b
  99.     # if not b:
  100.     #     return a
  101.  
  102.     if (not a) or (not b):  # if one is empty (or both are empty)
  103.         # if a:
  104.         #    return a
  105.         # else:
  106.         #    return b
  107.         return a + b
  108.  
  109.     # if a[0] <= b[0]:
  110.     #     return [a[0]] + merge_recursion(a[1:], b)
  111.     # else:
  112.     #     return [b[0]] + merge_recursion(a, b[1:])
  113.  
  114.     head_a, *tail_a = a
  115.     head_b, *tail_b = b
  116.  
  117.     if head_a <= head_b:
  118.         return [head_a] + merge_recursion(tail_a, b)
  119.     else:
  120.         return [head_b] + merge_recursion(a, tail_b)
  121.  
  122.  
  123. a = [1, 2, 3, 7, 8, 9]
  124. b = [1, 4, 6, 9, 12]
  125. print(f"{a = }")
  126. print(f"{b = }")
  127.  
  128. print(merge_1(a, b))
  129. print(merge_2(a, b))
  130. print(merge_recursion(a, b))
  131.  
  132. # check if original lists still exist (so `.pop()` didn't change them)
  133. print(f"{a = }")
  134. print(f"{b = }")
Advertisement
Add Comment
Please, Sign In to add comment