vendredi 21 juin 2019

Incomplete/Wrong result using remove method on simple Python lists

I have some code which removes items from a list (where each item is two words) based on items in another list (where each item is a single word). I am trying to remove items where the first word in the word pair appears in the second list.

The code I have written works, but only for certain words. I've tried adjusting the stages of for and if, but the result is still incomplete/wrong - it deletes some items, but leaves others which require removing. Here is the code:

referencelist = ['his','a','their','family']
activelist = ['the car','a dog','his bag','their house','her hometown','our vacation','his final exam','family movie']

for wordpair in activelist:
    leadword = wordpair.split(" ", 1)[0]
    if leadword in referencelist:
        activelist.remove(wordpair)
print(activelist)

I expected the result to be:

['the car', 'her hometown', 'our vacation']

But the actual result is:

['the car', 'his bag', 'her hometown', 'our vacation', 'family movie']

I'm not sure why it works for "his final exam" and not "his bag", nor why it doesn't work at all on "family movie". Could someone please help? Thanks.

Aucun commentaire:

Enregistrer un commentaire