Idiom for 'the lesser of the two'?

Russell E. Owen owen at nospam.invalid
Fri Oct 18 16:27:11 EDT 2002


In article <A6udnSqYRcfk-C2gXTWcqA at comcast.com>,
 Gordon Airport <uce at ftc.gov> wrote:

>There's got to be something simple I'm missing...
>You obviously can't do
>a < b ? a : b
>and this
>a if a < b else b
>is a syntax error. I started playing with lambda functions
>but I didn't have any luck there either.

min(a,b,c...) works great. The "min" function is very flexible:
- it works with any # of arguments 
- if there's just one argument and it is a list or tuple (or perhaps 
other kinds of collection) it will return the minimum value from the 
collection.

If you really want to have the < in there somewhere, you could try 
taking advantage of the fact that x < y is either 0 or 1 and x or y is 
either x or y, thus:
  ((a < b) * a) or b
probably does the job (yecch!).

-- Russell



More information about the Python-list mailing list