I have a question about my Python code to find the max value within a list. The functional code is as following:
def large(x):
if len(x) == 1:
return x.pop(0)
else:
temp = x.pop(0)
previous = large(x)
if previous >= temp:
return previous
else:
return temp
But before that, I tried:
def large(x):
if len(x) == 1:
return x.pop(0)
else:
temp = x.pop(0)
if large(x) >= temp:
return large(x)
else:
return temp
And it will return the error message as:
<ipython-input-74-dd8676a7c4e6> in large(x)
3 return x.pop(0)
4 else:
----> 5 temp = x.pop(0)
6 if large(x) >= temp:
7 return large(x)
IndexError: pop from empty list
The toy data would be:
inputlist = [6,1,3,2,3,4,5,6]
large(inputlist)
Thank you for your help in advance. I can't find the main cause of this error. As for me, these two codes are completely same.