How to get the desired result in python?
Base nested dictionary:
dictionary = {"aliceCo": {'name': "Cooper, Alice", 'group': 'finance'},
"bobDe": {'name': "Decker, Bob", 'group': 'hr'},
"caecilEl": {'name': "Elton, Caecil", 'group': 'sales'}
[many similar entrys...]
}
My attempt:
def get_result_list(dictionary, search_string):
return [[key for inner_val in val.values() if search_string in inner_val] for key, val in dictionary.items()]
my_result_list = get_result_list(dictionary, 'sales')
My result:
my_result_list = [[], [], ['caecilEl'], [], [], ...]
Desired result:
my_result_list = ['caecilEl']
I get that I have double lists from the return line, but I found no way to omit them. Also all the empty lists are not wanted. I could go over the result and fix it in another function, but I wish to do it in the inital function.
Thanks for any advice or hints on where to look for a solution!