11
d = {
    "local": {
        "count": 1,
        "health-beauty": {
            "count": 1,
            "tanning": {"count": 1}
        }
    },
    "nationwide": {"count": 9.0},
    "travel": {"count": 0}
}    

In this instance "nationwide" is the largest.

Code is below to make it easier to attach to scripts:

d = {'travel': {'count': 0}, 'local': {'count': 1, 'health-beauty': {'count': 1, 'tanning': {'count': 1}}}, 'nationwide': {'count': 9.0}}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
AlexZ
  • 244
  • 6
  • 15
  • Was looking at `print max(d, key=lambda x: x[1])` – AlexZ Oct 10 '12 at 23:16
  • 2
    you should have made local.tanning the biggest ... as it is none of the answers would find it... – Joran Beasley Oct 10 '12 at 23:21
  • If "tanning" was 10, and you need to catch that, you might need to flatten the dictionary. Try something like this [http://stackoverflow.com/questions/6027558/flatten-nested-python-dictionaries-compressing-keys](http://stackoverflow.com/questions/6027558/flatten-nested-python-dictionaries-compressing-keys) – Iliyan Bobev Oct 10 '12 at 23:27
  • Could you show an example? – sehe Oct 10 '12 at 23:42
  • @sehe I have subsequently realized that this should have been a comment. The reason that I ended up with posting an answer is that I lack the score for comments. Anyhow, here's the example of the result: `>>> x = { 'a':1, 'b':2, 'c':{ 'aa':11, 'bb':22, 'cc':{ 'aaa':111 } } } >>> {'_'.join(k):v for k,v in flattenDict(x).items()} {'a': 1, 'b': 2, 'c_aa': 11, 'c_bb': 22, 'c_cc_aaa': 111}` Let me know once you get this comment, so I can delete the answer. Cheers. – Iliyan Bobev Oct 11 '12 at 00:19
  • Dropping links as answers is frowned upon. At least show the gist of the answer, adapting it to the specific question. Otherwise, this is not an answer and should have been a comment (possibly a suggested duplicate) – sehe Oct 10 '12 at 23:57
  • @sehe check the top answer to [this](http://stackoverflow.com/a/6043835/1405399) question. It has example. – Iliyan Bobev Oct 10 '12 at 23:55

2 Answers2

13
>>> max(d, key=lambda x: d[x]['count'])
'nationwide'
JBernardo
  • 32,262
  • 10
  • 90
  • 115
  • 2
    this wont work .. say if your internal dict is biggest(since it doesnt even consider the nested dictionaries) – Joran Beasley Oct 10 '12 at 23:20
  • change `d['local']['tanning']["count"]` to `10` I will bet you that it doesnt return tanning ... – Joran Beasley Oct 10 '12 at 23:29
  • @JoranBeasley I know it. Just that each count (as I understand) is the sum of nested dictionaries. If `d['local']['tanning']['count']` is `10`, `d['local']['count']` is also `10`(or more)... So no need to check – JBernardo Oct 10 '12 at 23:30
  • @JBernardo exactly, I ++ each parent too! – AlexZ Oct 10 '12 at 23:42
1

This should work for nested dictionary:

def find_max(d, name=None):
    return max((v, name) if k == "count" else find_max(v, k) for k, v in d.items())

>>> find_max(d)
(9.0, 'nationwide')
defuz
  • 26,721
  • 10
  • 38
  • 60