I doubt that this is what you really wanted, but this is what you asked for, i.e. A one-page list comprehension that produces the same result as your for loop:
newls = [list(set(sublist).intersection(set(b))) for sublist in a] a = [[1,4], [17,33,2],[2,33]] b = [1,4,5,6] >>> c = [list(set(sublist).intersection(set(b))) for sublist in a] >>> c [[1, 4], [], []]
You probably don't need empty lists, so:
>>> c = filter(None, [list(set(sublist).intersection(set(b))) for sublist in a]) >>> c [[1, 4]]
Note that this does not give the expected result for the second case:
a = [[1,4], [17,33,2],[2,33]] d = [2,33] e = filter(None, [list(set(sublist).intersection(set(d))) for sublist in a]) >>> e [[33, 2], [33, 2]]
source share