Search code examples
pythonlistdictionarynestedlist-comprehension

Getting keys from nested dictionary without using for loops


My Problem: I have a nested dictionary that looks like this:

my_dict = {
    'type_1' : {
        'id1' : some_item,
        'id2' : some_item_2,
    },
    'type_2' :{
        'id3' : some_item3,
        'id4' : some_item4,
    }
}

I don't intent to add more nesting in those sub_dicts, so I think this should be fairly easy.

Now I want to get all to get all inner keys (everything that starts with 'id') in one list,
so that i get ['id1', 'id2', 'id3', 'id4]

The easy way would be to use two for loops, but I want it to be as concise as humanly possible.

My initial approach looks like this:
all_keys = [list(sub_dict.keys()) for sub_dict in my_dict.values()] which outputs
[['id1', 'id2'], ['id3', 'id4']] which is halfway to where I want to get. Adding an asterisk in front of list(sub_dict(keys)) raises a syntax error:
"iterable unpacking cannot be used in comprehension"

Any idea how I get all 'id'-strings in one unnested list with max one additional line of code?


Solution

  • Firstly you need to access to values of the dict then the keys of that value

    new_dict=[id for i in my_dict.values() for id in i.keys()]