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

How to effectively decompose a dictionary containing nested dictionaries

I have a dictionary that has thousands of keys and each key has a corresponding nested dictionaries. As a MWE, I’m showing a very simple structure.

original_dict = {
    'H0JKYXRWSN0D': {'type': 'A', 'name': '5GJ3VT3P'},
    'T3W8Z1G3ZJFS': {'type': 'B', 'name': '2QNVJGM6'},
    'ZJ7VYG1J33FJ': {'type': 'B', 'name': 'UJV0LEH2'},
    'DK0MBF8N4H1S': {'type': 'A', 'name': '8X0JBY1R'},
    'DZC6UHTQ93UO': {'type': 'C', 'name': '83B4FLJ3'}
}

My goal is to create individual lists for each unique type in the dictionary as:

{'A': ['H0JKYXRWSN0D', 'DK0MBF8N4H1S'], 'B': ['T3W8Z1G3ZJFS', 'ZJ7VYG1J33FJ'], 'C': ['DZC6UHTQ93UO']}

For instance, I would like to see all the keys that has type A. I can do this in a for loop, but was curious if there is a more efficient way to do like a built-in function that is faster than using a simple for loop. Here’s my current solution.

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

def create_lists_by_type(dictionary):
    lists_by_type = {}
    for key, value in dictionary.items():
        if value['type'] not in lists_by_type:
            lists_by_type[value['type']] = []
        lists_by_type[value['type']].append(key)
    return lists_by_type

>Solution :

you can use defaultdict just to reduce the work of checking the key and then creating value field and adding value, by directly appending value to keys.

Below is code, little clean.

>>> original_dict = {
...     'H0JKYXRWSN0D': {'type': 'A', 'name': '5GJ3VT3P'},
...     'T3W8Z1G3ZJFS': {'type': 'B', 'name': '2QNVJGM6'},
...     'ZJ7VYG1J33FJ': {'type': 'B', 'name': 'UJV0LEH2'},
...     'DK0MBF8N4H1S': {'type': 'A', 'name': '8X0JBY1R'},
...     'DZC6UHTQ93UO': {'type': 'C', 'name': '83B4FLJ3'}
... }
>>> 
>>> from collections import defaultdict
>>> result = defaultdict(list)
>>> 
>>> for k, v in original_dict.items():
...     result[v['type']].append(k)
... 
>>> result
defaultdict(<class 'list'>, {'A': ['H0JKYXRWSN0D', 'DK0MBF8N4H1S'], 'B': ['T3W8Z1G3ZJFS', 'ZJ7VYG1J33FJ'], 'C': ['DZC6UHTQ93UO']})
>>> 
>>> dict(result)
{'A': ['H0JKYXRWSN0D', 'DK0MBF8N4H1S'], 'B': ['T3W8Z1G3ZJFS', 'ZJ7VYG1J33FJ'], 'C': ['DZC6UHTQ93UO']}
>>> 

so you can use this function

from collections import defaultdict


def create_lists_by_type(dictionary):
    result = defaultdict(list)
    for k, v in dictionary.items():
        result[v['type']].append(k)
    return dict(result)
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