Search code examples
pythonlistmaxminlarge-data

Finding min()/max() of a 'large' list in python. ValueError: min() arg is an empty sequence


I have a list of lists say E[ ][ ], where E has ten sub-lists, each having around 500 entries.

My prime concern is to calculate the maximum of all the 5000 values, which are distributed in ten sub-lists.

Now, what I wrote was this:

MinVal = min(min(E[i]) for i in range(len(E)))

and it gave me this error: ValueError: min() arg is an empty sequence

Now I wrote this:

min_arr = []
for i in range(len(E)):
    min_arr.append(min(E[i]))
MinVal = min(min_arr)

and it gives me the same error: ValueError: min() arg is an empty sequence

So, I just try out doing this:

print(max(E[1]))

and it DOES give me the answer

The first two codes also work for small 5-10 element lists. But shows an issue with large data sets.

What should I do?


Solution

  • Your code:

    MinVal = min(min(E[i]) for i in range(len(E)))
    

    fails when E[i] == [], as there is no sensible definition of the minimum of an empty set. You therefore need to skip empty sub-lists. One option would be:

    min_val = min(min(e) for e in E if e)
    

    which is roughly equivalent to:

    min_vals = []
    for e in E:
        if e: # or 'if e != []:' - empty sequences evaluate False-y
            mins.append(min(e))
    min_val = min(min_vals)
    

    (Note that you're not actually using the index i anywhere, so you can iterate directly over E.)

    5,000 items isn't that many, you probably don't need to worry too much about efficiency.