Search code examples
pythonintegerminimum

finding minimum variable in Python


I have some integer variables and I want to find smallest one. When I use:

m1 = min(v1, v2, ...)

I get the value of the smallest one, not its name. I want to know which one is smallest, not know it's value! How should I do this?


Solution

  • If the index number will work, you could do this:

    # enter variables
    a = 1
    b = 2
    c = 3
    
    # place variables in list
    l = (a,b,c)
    
    # get index of smallest item in list
    X = l.index(min(l))
    
    # to print the name of the variable
    print(l[X])
    

    X, then, is the index number of the smallest variable (in this case, 0) and can be used as needed, or l[X] could be used to access the variable name.

    (But don't use lower case "L" like I did, not usually considered good style 'cuz it can easily be mistaken for upper cas "i" or the number 1).