1

In this code, I am trying to remove values (synonyms) within a list that are 7 or fewer characters from the dictionary. For some reason, my code is only partially removing the values that are 7 or fewer characters. Also, please do not remove any of the functions or use imports and sets to solve and keep the current code as intact as possible.

My current output:

{'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'], 
'dangerous': ['perilous', 'hazardous', 'uncertain']}

Desired output:

{'show' : ['demonstrate', 'indicate', 'point to'], 
'slow' : ['leisurely', 'unhurried'],
'dangerous' : ['hazardous', 'perilous', 'uncertain']}
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
             'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
             'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        for item in value:
            if len(item) <= 7:
                value.remove(item)
    return word_dict

main()
Noneiffy04
  • 135
  • 5
  • Why `note` is not considered in your desired output? As it is satisfying `<=7` condition? Can you please explain? – shaik moeed May 26 '19 at 06:33
  • Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – mkrieger1 May 26 '19 at 06:44

3 Answers3

2

You are modifying the same list you are iterating on when you do for item in value:.
Instead you need to iterate on value[:] which returns a copy of the array

word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        #Iterate on copy of value
        for item in value[:]:
            if len(item) <= 7:
                value.remove(item)
    return word_dict

main()

The output will be

{
'show': ['point to', 'indicate', 'demonstrate'], 
'slow': ['unhurried', 'leisurely'],
 'dangerous': ['perilous', 'hazardous', 'uncertain']
}

Another option is to create a new list, add words with len>7 to the list and assign the list to the key of the dictionary

 word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)


def remove_word(word_dict):
    for key, value in word_dict.items():

        #List of holding words with len more that 7
        li = []
        for item in value:
            #Add words with len more than 7 to the list
            if len(item) > 7:
                li.append(item)
        #Assign the list to the key
        word_dict[key] = li
    return word_dict

main()
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
2

Create a new list to filter the values with length > 7 and later assign it to the respective key.
Simply, You can modify your code like this:

word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
            'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        new_val = []
        for item in value:
            if len(item) > 7:
                new_val.append(item)
        word_dict[key] = new_val
    return word_dict

main()
xanjay
  • 561
  • 6
  • 20
1

This can be done using a dict and list comp:

edited_synonyms = {k: [w for w in v if len(w) >= 7] for k, v in word_dict.items()}

I suggest you use this instead of trying to modify a collection while iterating over it. It’s also more efficient than the other options present.

Jab
  • 26,853
  • 21
  • 75
  • 114