Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 553
HighAllegiant Avatar asked Feb 12 '26 15:02

HighAllegiant


1 Answers

Simple way:

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

or

list(set(secondlst).difference(lst))
like image 85
koblas Avatar answered Feb 15 '26 04:02

koblas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!