## Highest absolute Value in a Dictionary

GarraTheWinnie
Posts: 218
Joined: Tue Jul 28, 2020 10:34 am

### Highest absolute Value in a Dictionary

Hey everyone,

I am trying to get the highest absolute value from a dictionary similar to this:

Code: Select all

``````cVal = {'r'=50, 'b'=-55, c=40}
``````
in this case it will be 55

This is how I figured it out

Code: Select all

``````maxVal = abs(cVal[max(cVal, key = lambda y: abs(cVal[y]))])
``````
but that seems to be kinda redundant. I have to iterate through the dictionary twice, and use abs twice.

anyone have a any idea how to make this a little cleaner?

robbes
Posts: 153
Joined: Sun Jan 20, 2013 7:11 pm

### Re: Highest absolute Value in a Dictionary

If you don't need to know the key, you could do something like this:

Code: Select all

``````maxval = max([abs(x) for x in mydict.values()])
``````

GarraTheWinnie
Posts: 218
Joined: Tue Jul 28, 2020 10:34 am

### Re: Highest absolute Value in a Dictionary

robbes wrote:
Wed Apr 14, 2021 4:12 pm
If you don't need to know the key, you could do something like this:

Code: Select all

``````maxval = max([abs(x) for x in mydict.values()])
``````
Spot on! thank you so much!

scruss
Posts: 4027
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

### Re: Highest absolute Value in a Dictionary

From the TIMTOWTDI-EIP¹ department, but with a temporary dictionary:

Code: Select all

``````>>> cVal = {'r':50, 'b':-55, 'c':40}
>>> absVal={abs(x):x for x in cVal.values()}
>>> absVal[max(absVal.keys())]
-55
``````
You might want the sign returned intact, as if you're making control decisions on the value (temperature, say) it really, really matters whether it's -55 °C or 55 °C

There has to be a way to do it without a temporary dict, but it might not be too understandable.

---
¹: There Is More Than One Way To Do It — Even In Python
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

twostage
Posts: 87
Joined: Sun May 07, 2017 6:31 pm
Location: Northumberland

### Re: Highest absolute Value in a Dictionary

Code: Select all

``````sorted(list(cVal.values()))[-1]
``````
for signed. I'll have a think about an abs version.

scruss
Posts: 4027
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

### Re: Highest absolute Value in a Dictionary

The abs is the important bit. You could just do 'max(list(cVal.values()))' if you didn't need that
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

MiscBits
Posts: 195
Joined: Wed Jan 27, 2021 12:48 pm

### Re: Highest absolute Value in a Dictionary

Code: Select all

``````c = {'r':50, 'b':-55, 'c':40}
max(abs(c[max(c)]),abs(c[min(c)]))``````
Messy and a pain to type on an iPad

A more English walkthrough is:
a) Find the key with max value - max(c)
b) Find its value - c[]
c) Find the absolute value
d) Repeat for min
e) Find the max of those two

Note this does loose the sign but that was acceptable in the OPs request...

twostage
Posts: 87
Joined: Sun May 07, 2017 6:31 pm
Location: Northumberland

### Re: Highest absolute Value in a Dictionary

twostage wrote:
Thu Apr 15, 2021 4:40 pm

Code: Select all

``````sorted(list(cVal.values()))[-1]
``````
for signed. I'll have a think about an abs version.
Oops, that returns the smallest, should have been a zero not -1.

Interesting challenge.

jamesh
Raspberry Pi Engineer & Forum Moderator
Posts: 28432
Joined: Sat Jul 30, 2011 7:41 pm

### Re: Highest absolute Value in a Dictionary

One thing I have always wondered about with Python - lots of ways to do things, some more or less readable than others, but which is the most efficient? Would simply looping through all the keys manually with a for be faster than some of the more esoteric ways of doing it?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Application's Team.

twostage
Posts: 87
Joined: Sun May 07, 2017 6:31 pm
Location: Northumberland

### Re: Highest absolute Value in a Dictionary

jamesh wrote:
Fri Apr 16, 2021 9:52 am
One thing I have always wondered about with Python - lots of ways to do things, some more or less readable than others, but which is the most efficient? Would simply looping through all the keys manually with a for be faster than some of the more esoteric ways of doing it?
I know what you mean. Sometimes doing it the simplest but possibly most CPU intensive is best.

I've made a few rpi zero clocks recently using neopixels and scrollphats. My initial attempt at coding I calculated what should be on/off/What colour/how bright at start-up, then work out how it changes every second/minute/hour and do the updates. Eventually realised it was far easier to calculate everything every second (or every tenth of a second in one case).

scruss
Posts: 4027
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

### Re: Highest absolute Value in a Dictionary

There should be one-- and preferably only one --obvious way to do it.” in Python, which is obviously bunk. The best way is the one you understand how to write and works (for your current personal definition of 'works'). There's probably a clever and complete algorithm that does this, but the amount of time it would take to find it and get it in your code would be too long. For three entries, any solution will take almost no time. If there are millions of entries? Maybe a more efficient algorithm is called for - until your computer is replaced by something that does it in trivial time.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him