Thursday, 5 September 2013

how to delete a list within a list (i.e., a sublist) if any element of that sublist is in another list?

how to delete a list within a list (i.e., a sublist) if any element of
that sublist is in another list?

I have a list that contains a number of sublists. For example:
full_list = [[1, 1, 3, 4], [3, 99, 5, 2],[2, 4, 4], [3, 4, 5, 2, 60]]
I also have another list, called omit. For example:
omit = [99, 60, 98]
I want to remove the sublists inside of full_list, if any element in that
sublist is in the omit list. For example, I would want the resulting list
to be:
reduced_list = [[1, 1, 3, 4], [2, 4, 4]]
because only these sublists do not have an element that is in the omit list.
I am guessing that there is some easy way to pull this off with a list
comprehension but I cannot get it to work. I have tried a bunch of things:
For example:
reduced_list = [sublist for sublist in full_list if item for sublist not
in omit]
this code results in an error (invalid snytax) - but I think I'm missing
more than that.
Any help would be much appreciated!
p.s., The above is a simplified problem. My end goal is to remove sublists
from a very long list (e.g., 500,000 sublists) of strings if any element
(a string) of those sublists is in an "omit" list contain over 2000
strings.

No comments:

Post a Comment