Jonathan Jonathan - 2 months ago 20
Python Question

Python: min(None, x)

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?

Answer

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