Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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'}})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading