How can I use min() without getting "0" or "" for an answer?


I’m trying to use min in a list that came from a .csv and some of the values are how can I ignore those and also "0"?

I tried

index1 = (life_expectancy.index(min(life_expectancy,)))

and got nothing, when i tried:

index1 = (life_expectancy.index(min(life_expectancy, key=int)))

I got:

ValueError: invalid literal for int() with base 10: ”

Because this is the value that the function treats at the min

Asked By: LuigiMopi



I don’t think there is a simple way to do this in a single line of code using min.

One way to avoid 0-values is to call filter before min. Then, to avoid invalid values, you can write a wrapper around int that returns 0 on invalid values.

def int_or_zero(s):
        return int(s)
    except ValueError:
        return 0

def nonzero_min(seq):
    return min(filter(None, map(int_or_zero, seq)))

print( nonzero_min(['hello', '0', '12', '3', '0', '5', '']) )
# 3

Another way is to write the whole function "manually" with a for-loop.

def nonzero_min2(seq):
    m = 9999999
    for x in seq:
            x = int(x)
            if x < m and x != 0:
                m = x
        except ValueError:
    return m

print( nonzero_min2(['hello', '0', '12', '3', '0', '5', '']) )
# 3
Answered By: Stef

Try this:

new_life_expectancy = [value for value in life_expectancy if value != '' and value != '0']

This removes all instances of '' and 0

Answered By: Hunter
#You can simply remove all 0 terms and string terms from your list and then use min
while True:
    except ValueError:
Answered By: Ammar_Asim_23

The problem statement is incomplete, so we cannot conclude.

Is it a list of strings ? integers ? floats ? with None ? with float("NaN") ? a mix of all ?

With a list of mixed numbers and strings, you would have:

life_expectancy= [2,3,5,7,11,13, "", 0, "", 3.14, 1.414, "", 1.712, "", 1.618, 0]
index1 = (life_expectancy.index(min(life_expectancy,)))
TypeError: '<' not supported between instances of 'str' and 'int'

index1 = (life_expectancy.index(min(life_expectancy,key=int)))
ValueError: invalid literal for int() with base 10: ''

With a list of mixed numbers, None, and strings, you would have:

life_expectancy= [2,3,5,7,11,13, None, 0, None, 3.14, 1.414, None, 1.712, None, 1.618, 0]
index1 = (life_expectancy.index(min(life_expectancy,)))
TypeError: '<' not supported between instances of 'NoneType' and 'int'

index1 = (life_expectancy.index(min(life_expectancy,key=int)))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

So, yeah … it all depends on your actual inputs…
Stef’s answer should do the trick.


life_expectancy= [2,3,5,7,11,13, "", "0", "", "3.14", 1.414, "", 1.712, "", 1.618, 0]
new_list = [e for e in life_expectancy if e and type(e) != str]
>>> [2, 3, 5, 7, 11, 13, 1.414, 1.712, 1.618]
  • revise your CSV loading.

you may consider using pandas and its read_csv() function, quopting:

Read a comma-separated values (csv) file into DataFrame.

Also supports optionally iterating or breaking of the file into chunks.

Give a special look to all the doc/parameters, because there are a lot of default values and inferences, especially:

sepstr, default ‘,’

Delimiter to use.


decimalstr, default ‘.’

Character to recognize as decimal point (e.g. use ‘,’ for European data).

(and a lot more)

Answered By: LoneWanderer

you can try something like :

df_temp = df.loc[df["column_name"]!=""]

df_temp = df_temp.loc[df_temp["column_name"]>0]

Im not sure if you could just do != "" | 0, not sure if loc supports double condition.

Then you can just do a min on that df_temp

Answered By: Dava
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.