I have two lists in Python:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
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:
temp3 = ['Three', 'Four']
Are there any fast ways without cycles and checking?
Best Answer
To get elements which are in
temp1
but not intemp2
(assuming uniqueness of the elements in each list):Beware that it is asymmetric :
where you might expect/want it to equal
set([1, 3])
. If you do wantset([1, 3])
as your answer, you can useset([1, 2]).symmetric_difference(set([2, 3]))
.