Search code examples
pythoncomparemaxminimum

More pythonic way to find out a number is minimul to all the number


Is there any pythonic way to find out a variable is having a minimum value out of a bunch of variables. for example

In [5]: if d<c and d<b and d<a:
   ...:     print "d is minimum.."
   ...:     
d is minimum..

Now here only 3 variables are there, So we can do it using and, but what if there are so many variables there to compare ?
please tell me the case of checking at once d is maximum or not with all other variables.?
What about checking d is equal to all other variables ?
My solution:

May be we can add all the variable which needs to be compare in a list and compare them One by one, but I think there must be a better way to do this using python.


Solution

  • Using all which will be efficient as it will short-circuit:

    if all(d < i for i in [1,5,4,4,6,6,4,4,5]) 
    

    Where i can be any iterable

    Your example would be:

    if all(d < i for i in (c, b, a))