toy
toy

Reputation: 12161

Pythonic way to check if element in a list contains in key dictionary

I have a mapping keywords like this.

categories_mapping = {
        'comics': 'Comic Books',
        'cartoons': 'Comic Books',
        'manga': 'Comic Books',
        'video and computer games': 'Video Games',
        'role playing games': 'Video Games',
        'immigration': 'Immigration',
        'police': 'Police',
        'environmental': 'Environment',
        'celebrity fan and gossip': 'Celebrity',
        'space and technology': 'NASA / Space',
        'movies and tv': 'TV and Movies',
        'elections': 'Elections',
        'referendums': 'Elections',
        'sex': 'Sex',
        'music': 'Music',
        'technology and computing': 'Technology'}

and a list like this.

labels = ['technology and computing', 'arts and technology']

I want to return the value of the dictionary if any words in the list is in the key of the dictionary.

This is what I've come up with but I think that's not very pythonic.

cats = []
for k,v in categories_mapping.items():
    for l in labels:
        if k in l:
            cats.append(v)
return cats

The result I want is ['Technology']

Any better way of doing this?

Upvotes: 0

Views: 114

Answers (5)

btmcnellis
btmcnellis

Reputation: 712

You can get rid of the outer loop:

for l in labels:
    if l in categories_mapping:
        cats.append(categories_mapping[l])

Or as a list comp:

 cats = [v for k, v in categories_mapping if k in l]

Upvotes: 0

Eugene Soldatov
Eugene Soldatov

Reputation: 10145

You can use intersection of your labels and dictionary keys:

cats = [categories_mapping[key] for key in set(labels).intersection(categories_mapping)]

Update for partial match:

cats = [categories_mapping[key] for key in categories_mapping if any(label.lower() in key.lower() for label in labels)]

Upvotes: 6

Mayur Koshti
Mayur Koshti

Reputation: 1862

cats = [v for k, v in categories_mapping.items() if k in labels]

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 362197

>>> [categories_mapping[l] for l in labels if l in categories_mapping]
['Technology']

Upvotes: 1

R Nar
R Nar

Reputation: 5515

>>> categories_mapping = {
        'comics': 'Comic Books',
        'cartoons': 'Comic Books',
        'manga': 'Comic Books',
        'video and computer games': 'Video Games',
        'role playing games': 'Video Games',
        'immigration': 'Immigration',
        'police': 'Police',
        'environmental': 'Environment',
        'celebrity fan and gossip': 'Celebrity',
        'space and technology': 'NASA / Space',
        'movies and tv': 'TV and Movies',
        'elections': 'Elections',
        'referendums': 'Elections',
        'sex': 'Sex',
        'music': 'Music',
        'technology and computing': 'Technology'}
>>> labels = ['technology and computing', 'arts and technology']
>>> cats = []
>>> for l in labels:
    if l in categories_mapping:
        cats.append(categories_mapping[l])
>>> cats
['Technology']

Upvotes: 0

Related Questions