Count number of objects in list of dictionary where a key's value is more than 1

Given a list of dictionaries:

data = {
    "data": [
        {
            "categoryOptionCombo": {
                "id": "A"
            },
            "dataElement": {
                "id": "123"
            }
        },
        {
            "categoryOptionCombo": {
                "id": "B"
            },
            "dataElement": {
                "id": "123"
            }
        },
        {
            "categoryOptionCombo": {
                "id": "C"
            },
            "dataElement": {
                "id": "456"
            }
        }
    ]
}

I would like to display the dataElement where the count of distinct categoryOptionCombo is larger than 1.

e.g. the result of the function would be an iterable of IDs:

[123]

because the dataElement with id 123 has two different categoryOptionCombos.

tracker = {}
for d in data['data']:
    data_element = d['dataElement']['id']
    coc = d['categoryOptionCombo']['id']
    if data_element not in tracker:
        tracker[data_element] = set()
    tracker[data_element].add(coc)

too_many = [key for key,value in tracker.items() if len(value) > 1]

How can I iterate the list of dictionaries preferably with a comprehension? This solution above is not pythonic.

>Solution :

One approach:

import collections
counts = collections.defaultdict(set)

for d in data["data"]:
    counts[d["dataElement"]["id"]].add(d["categoryOptionCombo"]["id"])

res = [k for k, v in counts.items() if len(v) > 1]
print(res)

Output

['123']

This approach creates a dictionary mapping dataElements to the different types of categoryOptionCombo:

defaultdict(<class 'set'>, {'123': {'B', 'A'}, '456': {'C'}})

Leave a Reply