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

Daiyue Weng daiyueweng at gmail.com
Wed May 25 09:32:34 EDT 2016


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)


cheers



More information about the Python-list mailing list