Python: min(None, x)

Question:

I would like to perform the following:

a=max(a,3)
b=min(b,3)

However sometimes a and b may be None.
I was happy to discover that in the case of max it works out nicely, giving my required result 3, however if b is None, b remains None

Anyone can think of an elegant little trick to make min return the number in case one of the arguments in None?

Asked By: Jonathan Livni

||

Answers:

def max_none(a, b):
    if a is None:
        a = float('-inf')
    if b is None:
        b = float('-inf')
    return max(a, b)

def min_none(a, b):
    if a is None:
        a = float('inf')
    if b is None:
        b = float('inf')
    return min(a, b)

max_none(None, 3)
max_none(3, None)
min_none(None, 3)
min_none(3, None)
Answered By: Steve Mayne

You can use an inline if and an infinity as the default, as that will work for any value:

a = max(a if a is not None else float('-inf'), 3)
b = min(b if b is not None else float('inf'), 3)
Answered By: Blender

Why don’t you just create a generator without None values? It’s simplier and cleaner.

>>> l=[None ,3]
>>> min(i for i in l if i is not None)
3
Answered By: utdemir

Here is a decorator that you can use to filter out None values that might be passed to a function:

def no_nones(fn):
    def _inner(*args):
        return fn(a for a in args if a is not None)
    return _inner

print no_nones(min)(None, 3)
print no_nones(max)(None, 3)

prints:

3
3
Answered By: PaulMcG
a=max(a,3) if a is not None else 3
b=min(b,3) if b is not None else 3
Answered By: Steve Mitchell

@utdemir’s answer works great for the provided example but would raise an error in some scenarios.

One issue that comes up is if you have a list with only None values. If you provide an empty sequence to min(), it will raise an error:

>>> mylist = [None, None]
>>> min(value for value in mylist if value)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence

As such, this snippet would prevent the error:

def find_minimum(minimums):
    potential_mins = (value for value in minimums if value is not None)
    if potential_mins:
        return min(potential_mins)
Answered By: Kevin London

A solution for the Python 3

Code:

# variable lst is your sequence

min(filter(lambda x: x is not None, lst)) if any(lst) else None

Examples:

In [3]: lst = [None, 1, None]

In [4]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[4]: 1

In [5]: lst = [-4, None, 11]

In [6]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[6]: -4

In [7]: lst = [0, 7, -79]

In [8]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[8]: -79

In [9]: lst = [None, None, None]

In [10]: min(filter(lambda x: x is not None, lst)) if any(lst) else None

In [11]: print(min(filter(lambda x: x is not None, lst)) if any(lst) else None)
None

Notes:

Worked in sequence presents as numbers as well as None. If all values is None min() raise exception

ValueError: min() arg is an empty sequence

This code resolve this problem at all

Pros:

  1. Worked if None presents in sequence
  2. Worked on Python 3
  3. max() will be work also

Cons

  1. Need more than one non-zero variable in the list. i.e. [0,None] fails.
  2. Need a variable (example lst) or need duplicate the sequence
Answered By: PADYMKO

My solution for Python 3 (3.4 and greater):

min((x for x in lst if x is not None), default=None)
max((x for x in lst if x is not None), default=None)
Answered By: R1tschY

I believe the cleanest way is to use filter built-in function

a = max(filter(None, [a, 3]))
b = min(filter(None, [b, 3]))
Answered By: Sam

If you can use np.nan instead of None:

import numpy as np
float(np.nanmax([a, np.nan]))
float(np.nanmin([a, np.nan]))
Answered By: alEx
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.