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