0

I have a list of words and values, like:

{"word1" : 5, "word2" : 3, "word3", : 15, "word4" : 12}

I want to find and remove all the words that have a value less than 10, so it ends up like:

{"word3" : 15, "word4" : 12}

How do I do this? Just to say, I don't know the values in advance, so I can't just sort and trim some of it.

  • Nitpick: This is a dict of words and values, not a list. But dict comprehension is your friend, as Rakesh pointed out. – Jtcruthers Oct 10 '19 at 13:52

2 Answers2

1

Using a dict comprehension

Ex:

data = {"word1" : 5, "word2" : 3, "word3" : 15, "word4" : 12}
print({k:v for k, v in data.items() if v > 10})

Output:

{'word3': 15, 'word4': 12}
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

You can try this:

mydict = {"word1" : 5, "word2" : 3, "word3", : 15, "word4" : 12}
for key in list(mydict):
  if mydict[key] < 10:
    del mydict[key]
Emad
  • 71
  • 1
  • 3