Reputation: 21
Hi im looking for a way to remove duplicate of tuples from one list when compared to tuples in others different lists and/or lists of lists of tuples.
Example :
possible_cell = [(7,7),(3,3),(4,4),(5,5)]
wrong_path = [(1,1),(1,2),[(3,3)]]
current_path = [(4,4),[(5,5)]]
this_path = [(6,6)]
wanted :
new_possible_cell = [2-tuple for 2-tuple in possible_cell if 2-tuple not in wrong_path and 2-tuple not in current_path etc....]
expected return :
new_possible_cell = [(7,7)]
Upvotes: 2
Views: 35
Reputation: 177901
You're close, just flatten the list first.
possible_cell = [(7,7),(3,3),(4,4),(5,5)]
wrong_path = [(1,1),(1,2),[(3,3)]]
current_path = [(4,4),[(5,5)]]
this_path = [(6,6)]
def flatten(L):
for x in L:
if type(x) == list:
yield from flatten(x)
else:
yield x
new_possible_cell = [x for x in possible_cell if x not in flatten(wrong_path+current_path+this_path)]
print(new_possible_cell)
output:
[(7, 7)]
If your lists are large, use set(flatten(...))
for better speed.
Upvotes: 2