Posted By: Anonymous
I have two lists in Python, like these:
temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One', 'Two']
I need to create a third list with items from the first list which aren’t present in the second one. From the example I have to get:
temp3 = ['Three', 'Four']
Are there any fast ways without cycles and checking?
In : list(set(temp1) - set(temp2)) Out: ['Four', 'Three']
In : set([1, 2]) - set([2, 3]) Out: set()
where you might expect/want it to equal
set([1, 3]). If you do want
set([1, 3]) as your answer, you’ll need to use
set([1, 2]).symmetric_difference(set([2, 3])).