# Recursive merge sort - RecursionError: maximum recursion depth exceeded while calling a Python object

V

#### Vicgen

##### Guest
I'm very new in programming and Python, so please excuse in advance my lack of knowledge.

I’m trying to implement a recursive merge sort method in a class. However, when I try to run the program I get the following error:

RecursionError: maximum recursion depth exceeded while calling a Python object

This is because of left = self.merge(lst[:middle]) and right = self.merge(lst[middle:]).

Does someone know how this problem could be solved?

def merge(self, lst, reverse=False):

middle = len(lst) // 2
left = self.merge(lst[:middle])
right = self.merge(lst[middle:])

result = []
i, j = 0, 0

if not len(left) or not len(right):
return left or right

while i < len(left) and j < len(right):
if reverse:
if left > right[j]:
result.append(left)
i += 1
else:
result.append(right[j])
j += 1
else:
if left < right[j]:
result.append(left)
i += 1
else:
result.append(right[j])
j += 1

if i == len(left) or j == len(right):
result.extend(left[i:] or right[j:])
break

return result

def _merge_sort(self, reverse=False):

lst = list(self.unsorted_tuple)

if len(lst) < 2:
return lst

return self.merge(lst)