-1

I want to make a function called remove_short_synonyms() which is passed a dict as a parameter. The keys of the parameter dict are words and the corresponding values are lists of synonyms. The function removes all the synonyms which have less than 7 characters from each corresponding list of synonyms.

If this is the dict:

synonyms_dict = {'beautiful': ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']}

How can I get this as the output?

{'beautiful': ['dazzling', 'handsome', 'magnificent', 'splendid']}
Georgy
  • 12,464
  • 7
  • 65
  • 73
verdy
  • 89
  • 1
  • 3

5 Answers5

3

I think your question is more proper to be titled as Remove values from a list instead of dict.

You can use remove, del or pop to remove element in a python list. Difference between del, remove and pop on lists

Or in a more pythonic way, i think, is

dict['beautiful'] = [item for item in dict['beautiful'] if len(item)>=7]
Diansheng
  • 1,081
  • 12
  • 19
  • the answer from user1190882 is a more general solution for a dict object, instead of just a list object – Diansheng Oct 16 '17 at 09:10
2

Make use of dict comprehension and list comprehension.

synonyms_dict = {'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']}
synonyms_dict = {k:[v1 for v1 in v if len(v1) >= 7] for k, v in synonyms_dict.items()}
print(synonyms_dict)

# {'beautiful': ['handsome', 'dazzling', 'splendid', 'magnificent']}

​
Prasad
  • 5,946
  • 3
  • 30
  • 36
0

Assuming you have python>=3.x, a more readable solution for a beginner would be:

synonyms_dict = {'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']}

new_list = []
for key,value in synonyms_dict.items():
   for i in range(len(value)):
      if len(value[i]) >= 7:
         new_list.append(value[i])

synonyms_dict['beautiful'] = new_list
print(synonyms_dict)
Harman
  • 1,168
  • 1
  • 9
  • 19
0

Here's a function that modifies the existing dictionary rather than replacing it. This can be useful if you have multiple references to the same dictionary.

synonyms_dict = {
    'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']
}

def remove_short_synonyms(d, minlen=7):
    for k, v in d.items():
        d[k] = [word for word in v if len(word) >= minlen]

remove_short_synonyms(synonyms_dict)
print(synonyms_dict)

output

{'beautiful': ['handsome', 'dazzling', 'splendid', 'magnificent']}

Note that this code does replace the existing lists in the dictionary with new lists. You could keep the old list objects, if you really need to do that, by changing the assignment line to

d[k][:] = [word for word in v if len(word) >= minlen]

although that will be slightly slower, and there's probably no reason to do this.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0
def remove_short_synonyms(self, **kwargs):

dict = {}
  word_list = []

  for key, value in synonyms_dict.items():
    for v in value:
      if len(v) > 7:
        word_list.append(v)
    dict[key] = word_list

  print dict


remove_short_synonyms(synonyms_dict)
pylearner
  • 537
  • 2
  • 8
  • 26