Make an omit
set, check the intersection at each step of the iteration:
>>> full_list = [[1, 1, 3, 4], [3, 99, 5, 2],[2, 4, 4], [3, 4, 5, 2, 60]] >>> omit = [99, 60, 98] >>> omit = set(omit) # or just omit = {99, 60, 98} for python >= 2.7 >>> [item for item in full_list if not omit & set(item)] [[1, 1, 3, 4], [2, 4, 4]]
FYI, it's better to use frozenset
instead of the set suggested by @ Óscar López. With frozenset
it works a little faster:
import timeit def omit_it(full_list, omit): return [item for item in full_list if not omit & set(item)] print timeit.Timer('omit_it([[1, 1, 3, 4], [3, 99, 5, 2],[2, 4, 4], [3, 4, 5, 2, 60]], {99, 60, 98})', 'from __main__ import omit_it').timeit(10000) print timeit.Timer('omit_it([[1, 1, 3, 4], [3, 99, 5, 2],[2, 4, 4], [3, 4, 5, 2, 60]], frozenset([99, 60, 98]))', 'from __main__ import omit_it').timeit(10000)
prints:
0.0334849357605 0.0319349765778