1

I'm fairly new to Python and I'm struggling with nested dictionaries. Consider this dictionary of dictionaries:

dict=[{'Item 1': {'A': 106,
  'B': 77,
  'C': 46,
  'D': 36},
 'Item 2': {'E': 1141,
  'F': 1065,
  'G': 1020}}]

I would like to list the keys and then the count of the nested dictionary keys. So like this:

Item 1: 4
Item 2: 3

Thank you!

Paul
  • 135
  • 2
  • 11
  • You can use `len()` to get the number of keys in a dict: https://stackoverflow.com/questions/2212433/counting-the-number-of-keywords-in-a-dictionary-in-python – pault May 18 '18 at 18:39
  • 1
    `for k,v in dict.iteritems(): print(k,':',len(v))` **NOTE** use `.items()` for python 3.x. `iteritems()` is a python 2.x thing. – Aaron May 18 '18 at 18:39
  • or `{i:len(d[0][i]) for i in d[0].keys()}` – anishtain4 May 18 '18 at 18:40
  • 2
    Never call a variable like a [built-in function like dict()](https://docs.python.org/3.3/library/functions.html) – Mr. T May 18 '18 at 18:43
  • @pault Thanks. I could get the count of keys for the top level dictionary. len(dict), in this case. I needed the count of keys from the nested dictionaries. That was the difference. – Paul May 18 '18 at 21:28

3 Answers3

1
for k,v in lst[0].items():                                                                                                                                                        
    print(k, ':', len(v))
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
0

One idea:

[(k, len(v)) for k, v in dict[0].items()]

Output:

[('Item 1', 4), ('Item 2', 3)]

PS. I'd recommend not to name your dictionary dict - it is a reserved keyword and by doing so you shadow it.

Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53
  • This is broken on multiple levels. Your printed output isn't what the OP asked for, and the OP has the dictionary contained within a list so you'll get `AttributeError` trying to use `.items()` – roganjosh May 18 '18 at 18:44
  • Copy & paste error. And true about accessing the list, but the list was simply a single element, not adding much. Will fix for consistency. – Lukasz Tracewski May 18 '18 at 18:47
  • Thanks for the heads up about "dict." I wasn't using that term in my real file, but good to know so I never do. – Paul May 18 '18 at 21:25
0

When working with a nested dictionary within a list, you must first iterate through the list values. The list values are simply the dictionaries inside of your list. Once loop through the list items you can then loop through the elements of the dictionary. In the code below I looped through each item in the list, and then through the key value pairs to get the output that you are looking for.

dictionary = [{'Item 1': {'A': 106,
               'B': 77,
               'C': 46,
               'D': 36},
               'Item 2': {'E': 1141,
               'F': 1065,
               'G': 1020}}]

for item in dictionary:
      for key, value in item.items():
             print('{}:{}'.format(key, len(value)))

And here is your output:

Item 1:4
Item 2:3
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27