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.

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']] = []
    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():
    return dict(result)

Leave a Reply