0

Relative newbie to Python. I was trying to write a function to compare two lists and remove any duplicate values:

>>> michael = ['Fox', 'Jackson', 'Jordan']
>>> not_michael = ['Smith', 'Brown', 'Jordan', 'Fox']
>>> result = michael_remover(michael, not_michael)
>>> result
['Smith', 'Brown']

However, this is what I kept getting:

>>>result
['Smith', 'Brown', 'Fox']

This was my original code for the function:

def michael_remover(michael, not_michael):
    for name in not_michael:
        if name in michael:
            not_michael.remove(name)
    return not_michael

I eventually rewrote the function, and it worked as desired:

def michael_remover(michael, not_michael):
    not_michael_2 = [name for name in not_michael if name not in michael]
    return not_michael2

However, I am curious why my original code did not work. Can anyone correct / explain? It's on the tip of my brain, but I can't put it into words... Thanks

1 Answers1

3

You are changing the list as you iterate over it. That causes the unexpected behaviour you see. Internally the for-loop keeps track of the index you are on. When you remove an item, the next item is not processed because its index is now the index you just processed.

The list comprehension works because it creates a new list, rather than modifying the existing list. So it processes each element, and works as expected.

dsh
  • 12,037
  • 3
  • 33
  • 51