Home > Software design >  Counting occurrences in list of dicts
Counting occurrences in list of dicts

Time:11-10

I have a long list of dicts in the following structure:

[{'key1': 'dynamic_value', 'validlink': ['dynamic_value']}, {'key1': 'dynamic_value', 'validlink': ['dynamic_value']}]

I need to count how many values the validlink array holds for each key1 value(changing value).

I tried playing with a few counting approaches with no luck. Thanks!

CodePudding user response:

Is this what you are after? A more concrete example will be useful.

data = [{'key1': 'A', 'validlink': ['A','AA']}, {'key1': 'B', 'validlink': ['B']}]

for a in data:
   print('Key:', a['key1'], 'Count:', len(a['validlink']))
  • Related