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

Get a list of keys that match each item of another given list…?

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…

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

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]
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