user3712563
user3712563

Reputation: 59

Comparing two lists and filtering out duplicates

Maybe this is just from mental exhaustion, but I can not for the life of me figure this out, even though i used the same principle on another program I created..

I have two lists:

compare_list = [0,1,1,2,3,3,4,7,5,8,9,9]

master_list = [0,1,2,3,4,8,9]

As you can see both lists contain some numbers that are the same, and in compare_list you have values that have duplicates..

What I want completed is to compare both lists, and delete from the compare_list if it finds it in the master_list.

This is the code i have so far:

for x in compare_list:
    for y in master_list:
        if x == y:
            compare_list.remove(x)

The result is that i do have some items being deleted from compare_list, but i still have some duplicates left..

output:

  print(compare_list)

 [1,3,7,5,9]

how do i get it right where it deletes all instances of duplicates from master_list. so that compare_list just contains numbers that aren't found in master_list?

Upvotes: 1

Views: 204

Answers (4)

Sylvain Leroux
Sylvain Leroux

Reputation: 52070

Why not using collections.Counter:

import collections

compare_list = [0,1,1,2,3,3,4,7,5,8,9,9]
master_list = [0,1,2,3,4,8,9]

comp_set = collections.Counter(compare_list)
master_set = collections.Counter(master_list)

print comp_set-master_set

Producing:

Counter({1: 1, 3: 1, 9: 1, 5: 1, 7: 1})

Please beware that substract() might lead to 0 or negative counts. So to use effectively your result you probably have to add an extra pass:

print [k for k,v in (comp_set-master_set).items() if v > 0]
#                                                 ^^^^^^^^ 

Producing:

[1, 3, 9, 5, 7]

Upvotes: 0

Alessandro Suglia
Alessandro Suglia

Reputation: 1927

As suggested by the python official documentation list.remove "Remove the first item from the list whose value is x. It is an error if there is no such item."

So if you have duplicates in your compare_list you won't remove them.

If you already have a list and you need to remove duplicates from it I think that you need to convert your list in a set:

compare_list = [1,2,2,3,4,5,5,5]
my_set = set(compare_list)

If you need to filter the compare_list removing some specific elements you can do this step after you've transformed the list into a set using the set's method remove.

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 118021

Seems like a straight-forward use case for filter

>>> compare_list = [0,1,1,2,3,3,4,7,5,8,9,9]
>>> master_list = [0,1,2,3,4,8,9]
>>> filter(lambda i: i not in master_list, compare_list)
[7, 5]

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304503

compare_list = [x for x in compare_list if x not in master_list]

If master_list has more than a few items, it will be more efficient to use a set

master_set = set(master_list)
compare_list = [x for x in compare_list if x not in master_set]

Upvotes: 0

Related Questions