Niche.P Niche.P - 8 months ago 33
Python Question

Removing values in Dictionary with a list of words python

Let's say I have a list of words

nottastyfruits = ['grape', 'orange', 'durian', 'pear']

fruitGroup = {'001': ['grape','apple', 'jackfruit', 'orange', 'Longan'],
'002': ['apple', 'watermelon', 'pear']}

I want to go through all the keys in the dictionary and remove the words from nottastyfruits list.

My current code is

finalfruits = {}
for key, value in fruitGroup.items():
fruits = []
for fruit in value:
if fruit not in nottastyfruits:
finalfruits[key] = (fruits)

This takes so long to run when you have a large data text such as large text preprocessing. Is there a more efficient and faster way to do this?

Thank you for you time


Making it flat by using a dictionary comprehension will remove the overhead of the for loop.

Making nottastyfruits a set will decrease lookup time:

nottastyfruits  = set(nottastyfruits)
finalfruits = {k: [f for f in v if f not in nottastyfruits] for k, v in fruitGroup.items()}