Find the max number of elements in lists as value in a dictionary

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Wed May 25 10:24:10 EDT 2016


Daiyue Weng writes:

> I want to find the maximal number of elements contained in a nested
> dictionary, e.g.
>
> data = {
>         'violations':
>          {
>             'col1': {'err': [elem1, elem2, elem3]},
>             'col2': {'err': [elem1, elem2]}
>          }
>         }
>
> so to find the maximal number of elements in the lists for key 'err' in key
> 'col1' and 'col2'. Also key 'violations' may contain many keys (e.g. 'col1'
> , 'col2', 'col3' etc), so what's the best way to do this (using a loop)?
>
> max = 0for col in data.violations:
>    if max < len(data.violations.col.err):
>       max = len(data.violations.col.err)

Write a generator function that produces an object that generates the
lengths. The magic word is the "yield" which is used like "return" but
makes it so that the resulting objects yields each value on demand. For
greater magic, make it yield the path to each list together with the
length (length first, for easy max):

def errcounts(data):
    for top, sub in data.items():
        for mid, wev in sub.items():
            if 'err' in wev:
                yield (len(wev['err']), top, mid)

With that, Python's max does it all:

max(errcounts(data)) => (3, 'violations', 'col1')

This anticipated a next question; tuple comparison is overly specific;
use a key function to max to not overspecify if you care; max needs a
default in case there is no data; this same thing can be done with a
single "generator expression" and that might be good, depending on the
actual details and taste. Many other caveats apply.



More information about the Python-list mailing list