So I would like to get the maximum value from 3 variables, x
,y
,z
.
x = 1
y = 2
z = 3
max(x, y, z) # returns 3 but I want "z"
However this returns the value of z
i.e 3
. How do I get the name of the variable e.g. "z"
instead?
Thanks
So I would like to get the maximum value from 3 variables, x
,y
,z
.
x = 1
y = 2
z = 3
max(x, y, z) # returns 3 but I want "z"
However this returns the value of z
i.e 3
. How do I get the name of the variable e.g. "z"
instead?
Thanks
Make a dictionary and then use max
. Also works with min
. It will return you the variable name in the form of string.
>>> x = 1
>>> y = 2
>>> z = 3
>>> var = {x:"x",y:"y",z:"z"}
>>> max(var)
3
>>> var.get(max(var))
'z'
>>> var.get(min(var))
'x'
>>>