my_dict = {
'NONE': ['N', 'NE'],
'VERY SLIGHT': ['VSLT', 'VT'],
'SLIGHT': ['SLT', 'ST'],
'FAINT': ['F', 'FT'],
'MEDIUM': ['M', 'MM'],
'STRONG': ['S', 'SG'],
'VERY STRONG': ['VS', 'VG']
}
search_list = ['N', 'SLT', 'MM']
result_list = []
I want to search each item of search_list in values of my_dict and return the matching keys as list.
I need… result_list = [‘NONE’, ‘SLIGHT’, ‘MEDIUM’]
I got it by…
for i in search_list:
for k, v in my_dict.items():
if i in v:
result_list.append(k)
how to get it in more simpler and quickest way to do it?
>Solution :
Nested comprehension is one option.
[k for k, v in my_dict.items() for x in search_list if x in v]