Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of variable with highest integer from list Python 3.3.4

Tags:

python

list

Recently tried something similar to PHP get largest number from 3 variables

(In Python's form of course) However when returning the value I get the value of the variable, not the name of the variable.

My array looks like this:

x = 1

y = 2

z = 3

alloutputs = [x, y, z]

The furthest and most ugliest before needing help is this:

alloutputs[alloutputs.index(max(alloutputs))]

However it's still giving me an integer of the highest value! How would I get back the name of x, y, or z depending on which is largest?

like image 653
sunshinekitty Avatar asked Dec 07 '22 03:12

sunshinekitty


1 Answers

As @zhangxaochen says, you need to use a dictionary or named tuple. You could use a dictionary like this:

>>> d = {'x': 1, 'y': 2, 'z': 3}
>>> max(d, key=d.get)
'z'
like image 141
Noel Evans Avatar answered May 20 '23 12:05

Noel Evans