I have two lists
list1 = ['B', 'C', 'D']
list2 = [['1', 'A'], ['2', 'B'], ['3', 'D'], ['5', 'C']]
and want to return those sublistst of list2 that contain elements of list1. So far I tried using any:
result = []
for l2 in list2:
if any (item in l2 for item in list1):
result.append(l2)
and a naive approach
for l2 in list2:
for l1 in l1:
if l1 in l2:
result.append(l2)
But I only managed to repeat empty list. The result should be
result = [['2', 'B'], ['5', 'C']]
Not sure, where I'm going wrong. Is there maybe a way using list comprehensions or mixing list comprehensions and 'any' function?
['3', 'D']?for l1 in list1: