Python Forum

Full Version: error: max() arg is an empty sequence
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, 

I'm trying to compile this code:
for idx, i in enumerate(array):
    if  idx > phaseshift:
        if  abs(i - array[idx - phaseshift]) > 1000 or True:
            self.phasespace_x.append(array[idx - phaseshift])
            self.phasespace_y.append(i + array[idx - phaseshift])
            self.linx = [max(self.phasespace_x),min(self.phasespace_x)]
where the problem was at first in array variable. It was not defined. To reduce the error in the "enumerate " line I defined array as empty list as: array=[]. Because when I defined array as integer I got an error that integer in enumerate cannot be interated . 

Now I have this:
Error:
ValueError: max() arg is an empty sequence
which is quite logic I think because of empty variable array. Could you please tell me how to fix this problem? 

Thank you.
You could give a default value that's returned by max If the iterable is empty.
values = []
print(max(values, default=0))
Output:
0
(Oct-10-2016, 08:17 PM)Yoriz Wrote: [ -> ]You could give a default value that's returned by max If the iterable is empty.
values = []
print(max(values, default=0))
Output:
0

Thank you. Problem removed. :)