mellejgr
mellejgr

Reputation: 43

Remove list from list of lists if criterion is met

I am looking to program a small bit of code in Python. I have a list of lists called "keep_list", from which I want to remove any sublist containing a specific value found in another list called "deleteNODE".

for example:

deleteNODE=[0,4]
keep_list=[[0,1,2],[0,2,3],[1,2,3],[4,5,6]]

After running the code the result should be (removing any list containing 0 or 4):

keep_list=[[1,2,3]]

Is there any efficient way of doing this?

Upvotes: 2

Views: 1303

Answers (3)

Marcelus Trojahn
Marcelus Trojahn

Reputation: 709

I did it like this:

[x for x in keep_list if not set(x).intersection(deleteNODE)]

Since I thought the other answers were better, I also ran timeit on all the 3 answers and surprisingly this one was faster.

Python 3.8.2
>>> import timeit
>>>
>>> deleteNODE=[0,4]
>>> keep_list=[[0,1,2],[0,2,3],[1,2,3],[4,5,6]]
>>>
>>>
>>> def v1(keep, delete):
...     return [l for l in keep_list if not any(n in l for n in deleteNODE)]
...
>>> def v2(keep, delete):
...     return [i for i in keep_list if len(set(i)&set(deleteNODE)) == 0]
...
>>> def v3(keep, delete):
...     return [x for x in keep_list if not set(x).intersection(deleteNODE)]
...
>>>
>>> timeit.timeit(lambda: v1(keep_list, deleteNODE), number=3000000)
7.2224646
>>> timeit.timeit(lambda: v2(keep_list, deleteNODE), number=3000000)
7.1723587
>>> timeit.timeit(lambda: v3(keep_list, deleteNODE), number=3000000)
5.640403499999998

I'm no Python expert so can anyone understand why mine was faster since it appears to be creating a new set for every evaluation?

Upvotes: 3

GabrielC
GabrielC

Reputation: 320

This could be done using list comprehension

deleteNODE=[0,4]
keep_list=[[0,1,2],[0,2,3],[1,2,3],[4,5,6]]
...
keep_list = [l for l in keep_list if not any(n in l for n in deleteNODE)]

Upvotes: 1

LTheriault
LTheriault

Reputation: 1230

You can solve this using a list comprehension to cycle through each list within the larger list and by using sets. The & operator between sets returns the intersection (the elements common between both sets). Therefore, if the intersection between the list you're evaluating and deleteNODE is not zero, that means there is a common element and it gets excluded.

keep_list = [i for i in keep_list if len(set(i)&set(deleteNODE)) == 0]

Upvotes: 2

Related Questions