Reputation: 15
I am trying with a simple merge function and leave out the sorting orders in function. The Problem is here I am getting List Index out of Range Error in 20 line, elif (lowList[0] >= highList[0])
.
I am checking, if any of the lists are empty in the if
statements before this line, so this should not get executed if they are empty. But, Python checks for that, I think. Is there any workaround to overcome this.
def merge(my_l, low, high):
middle = int((low + high) / 2)
print(middle)
lowList = my_l[low : middle]
highList = my_l[middle + 1 : high]
my_indx = low
fullList = []
while (lowList is not [] or highList is not []):
if lowList is []:
fullList.append(highList.pop(0))
elif highList is []:
fullList.append(lowList.pop(0))
elif (lowList[0] >= highList[0]):
fullList.append(lowList.pop(0))
else:
fullList.append(highList.pop(0))
return fullList
san = [2, 3, 4, 5, 6, 7]
san = merge(san, 0, len(san) - 1)
print (len(san - 1))
for i in san:
print(i)
Thanks for the help.
Regards, S
Upvotes: 0
Views: 62
Reputation: 7821
The is
keyword checks for identity
, not equality.
Change your to ==
in the elif
's and '!=' in the while loop, and this error should not appear.
To see this in action, consider this code:
a = []
b = []
print id(a)
print id(b)
print a is b
print a == b
which outputs
33444128
33415256
False
True
Upvotes: 1