I am trying to remove duplicates from 2 lists. so I wrote this function:
a = ["abc", "def", "ijk", "lmn", "opq", "rst", "xyz"]
b = ["ijk", "lmn", "opq", "rst", "123", "456", ]
for i in b:
if i in a:
print "found " + i
b.remove(i)
print b
But I find that the matching items following a matched item does not get remove.
I get result like this:
found ijk
found opq
['lmn', 'rst', '123', '456']
but i expect result like this:
['123', '456']
How can I fix my function to do what I want?
Thank you.
To remove the common elements from two lists:Use the set() class to convert the first list to a set object. Use the symmetric_difference() method to remove the common elements. The method will return all elements that are in exactly one of the lists.
Using the groupby function, we can group the together occurring elements as one and can remove all the duplicates in succession and just let one element be in the list. This function can be used to keep the element and delete the successive elements with the use of slicing.
Your problem seems to be that you're changing the list you're iterating over. Iterate over a copy of the list instead.
for i in b[:]:
if i in a:
b.remove(i)
>>> b
['123', '456']
But, How about using a list comprehension instead?
>>> a = ["abc", "def", "ijk", "lmn", "opq", "rst", "xyz"]
>>> b = ["ijk", "lmn", "opq", "rst", "123", "456", ]
>>> [elem for elem in b if elem not in a ]
['123', '456']
Here is what's going on. Suppose you have this list:
['a', 'b', 'c', 'd']
and you are looping over every element in the list. Suppose you are currently at index position 1:
['a', 'b', 'c', 'd']
^
|
index = 1
...and you remove the element at index position 1, giving you this:
['a', 'c', 'd']
^
|
index 1
After removing the item, the other items slide to the left, giving you this:
['a', 'c', 'd']
^
|
index 1
Then when the loop runs again, the loop increments the index to 2, giving you this:
['a', 'c', 'd']
^
|
index = 2
See how you skipped over 'c'? The lesson is: never delete an element from a list that you are looping over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With