Removing common items between two lists

Possible duplicate:
Python List Subtraction Operation

I want to remove common items between two lists. I mean something like this


a=[1,2,3,4,5,6,7,8] b=[2,4,1] # I want the result to be like res=[3,5,6,7,8] 

Is there any simple pythonic way to do this?

+6
source share
2 answers

use sets:

 res = list(set(a)^set(b)) 
+19
source

The set has methods: http://docs.python.org/3/library/stdtypes.html#set

print (set (a) .difference (b))

0
source

All Articles