I have two list l1 and l2 which contains tuples inside, I need to filter out the tuple in l1 with the tuple present in l2. How to do this?
l1=[('a','b','c','d','e'),('t','y','u','i','o'),('q','s','a','e','r'),('t','f','e','w','l')]
l2=[('a','r'),('l','f')]
output=[('a','b','c','d','e'),('t','y','u','i','o')]
k=0
p=0
filter=[]
for i in l1:
for j in l2:
if i[k]!=j[p]:
ss=i
filter.append(ss)
k =1
p =1
CodePudding user response:
It would be more efficient to convert l2
into a list of sets first so that you can perform subset checks in linear time:
s2 = list(map(set, l2))
output = [l for l in l1 if not any(s.issubset(l) for s in s2)]
output
becomes:
[('a', 'b', 'c', 'd', 'e'), ('t', 'y', 'u', 'i', 'o')]