Skip to content
Advertisement

Get difference between two lists with Unique Entries

I have two lists in Python:

JavaScript

Assuming the elements in each list are unique, I want to create a third list with items from the first list which are not in the second list:

JavaScript

Are there any fast ways without cycles and checking?

Advertisement

Answer

To get elements which are in temp1 but not in temp2 (assuming uniqueness of the elements in each list):

JavaScript

Beware that it is asymmetric :

JavaScript

where you might expect/want it to equal set([1, 3]). If you do want set([1, 3]) as your answer, you can use set([1, 2]).symmetric_difference(set([2, 3])).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement