[Python-ideas] Have max and min functions ignore None

Chris Angelico rosuav at gmail.com
Mon Dec 28 23:21:57 EST 2015


On Tue, Dec 29, 2015 at 3:08 PM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> What do people think about having `max` and `min` ignore `None`?
>
> Examples:
>     max(1, None) == 1
>     min(1, None) == 1
>     max([1, None]) == 1
>     max(None, None) == max() or max(None, None) == max([])
>
> (The last one currently throws two different errors.)

What you could do is simply filter them out:

def max_without_none(seq):
    return max(item for item in seq if item is not None)

Would that do what you need?

ChrisA


More information about the Python-ideas mailing list