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?
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With