Skip to content
Advertisement

Python Filtering 2 Lists

I’m trying to find a way to use one list to filter out elements of another.

Kinda like the intersect syntax but the exact opposite

lst = [0,1,2,6]

secondlst = [0,1,2,3,4,5,6]

expected outcome

[3,4,5]

Advertisement

Answer

Simple way:

r = [v for v in secondlst if v not in lst]

or

list(set(secondlst).difference(lst))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement