Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the common elements between two lists [duplicate]

Tags:

python

Possible Duplicate:
Python list subtraction operation

I want to remove the common elements 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 ?

like image 244
OneMoreError Avatar asked Dec 02 '12 18:12

OneMoreError


Video Answer


1 Answers

use sets :

res = list(set(a)^set(b))
like image 64
dugres Avatar answered Oct 02 '22 14:10

dugres