7

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

Nouman
  • 6,947
  • 7
  • 32
  • 60
oceandye
  • 309
  • 3
  • 6
  • 14
  • the closest you can get is np.argmax; but that only returns the position in an array – Mohammad Athar Sep 06 '18 at 13:18
  • 6
    Variables are name mappings to objects, they don't really exist as strings. What are you attempting to do? – esqew Sep 06 '18 at 13:19
  • Is there a specific reason you need that information? What if multiple variables had the same value? What if one variable with the same value was never given to the max function? – OneCricketeer Sep 06 '18 at 13:19
  • 2
    You should use dictionary if you really want to get the name of maximum variable. `d = {'x': 1, 'y': 2, 'z': 3}` then `max(d, key=d.get)` – Harshita Sep 06 '18 at 13:20
  • This might help https://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string – Space Impact Sep 06 '18 at 13:28
  • I'm trying to find the mode for different categorical responses like yes, maybe, most likely, etc. So I would need the variable name since the actual value would be kinda useless. – oceandye Sep 06 '18 at 13:35
  • dict = { 'a':1,'b':2, 'c':3} # have to make a dictionary for that to print name instead of values. print(max(dict.keys())) # if want to print values then dict.values() – Harshit Varshney Sep 05 '22 at 17:09

6 Answers6

10

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'
>>>
Nouman
  • 6,947
  • 7
  • 32
  • 60
7

You could put them into a dictionary and use max with a key:

dict1 = {'x':1, 'y':2, 'z':3}
max(dict1, key=dict1.get)

But think through the ramifications of this. What if you have the same val multiple times, and other edge cases.

dfundako
  • 8,022
  • 3
  • 18
  • 34
2

Use a dictionary and get the maximum of its items, using a key function.

d = {'x':1, 'y':2, 'z':3}
max(d.items(), key=lambda i: i[1])
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

This is not at all recommended but you can use the globals() dictionary which keeps track of all variables that are either built-ins or have been defined by the user:

x = 1
y = 2
z = 3

variables = {k: v for k, v in globals().items() if isinstance(v, int)}
max_value = max(variables.values())
res = next(k for k, v in variables.items() if v == max_value)

which results in:

print(res)  # z
Ma0
  • 15,057
  • 4
  • 35
  • 65
  • What if in the program there are other globals with higher value than the 3 ones that we're comparing? – Natacha Sep 27 '20 at 14:09
0

After using max, you can write if statements.

> highest = max(x,y,z)
> 
> if highest == x:
  print('x')

and so forth

-3

Create a List of the numbers then type max() around the list.

ashish trehan
  • 413
  • 1
  • 5
  • 9