python remove duplicates from 2 lists -
i trying remove duplicates 2 lists. wrote function:
a = ["abc", "def", "ijk", "lmn", "opq", "rst", "xyz"] b = ["ijk", "lmn", "opq", "rst", "123", "456", ] in b: if in a: print "found " + b.remove(i) print b
but find matching items following matched item not remove.
i result this:
found ijk found opq ['lmn', 'rst', '123', '456']
but expect result this:
['123', '456']
how can fix function want?
thank you.
here what's going on. suppose have list:
['a', 'b', 'c', 'd']
and looping on every element in list. suppose @ index position 1:
['a', 'b', 'c', 'd'] ^ | index = 1
...and remove element @ index position 1, giving this:
['a', 'c', 'd'] ^ | index 1
after removing item, other items slide left, giving this:
['a', 'c', 'd'] ^ | index 1
then when loop runs again, loop increments index 2, giving this:
['a', 'c', 'd'] ^ | index = 2
see how skipped on 'c'? lesson is: never delete element list looping over.
Comments
Post a Comment