Python - eliminating common elements from two lists -
if have 2 lists:
a = [1,2,1,2,4] , b = [1,2,4]
how
a - b = [1,2,4]
such 1 element b removes 1 element if element present in a.
you can use itertools.zip_longest zip lists different length use list comprehension :
>>> itertools import zip_longest >>> [i i,j in izip_longest(a,b) if i!=j] [1, 2, 4]
demo:
>>> list(izip_longest(a,b)) [(1, 1), (2, 2), (1, 4), (2, none), (4, none)]
Comments
Post a Comment