I have 2 lists:
list_1 = [[1,1], [1,3], [1,1], [1,4]]
list_2 = ["string_1", "string_2", "string_3", "string_4"]
My goal is to remove the duplicate sublists from list_1
and also the strings in list_2
that have the same index as the sublists removed, while maintaining the order of the sublists.
I found that I can remove duplicate sublists while maintaining the order of the sublists in this SO using:
from itertools import *
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
seen = set()
seen_add = seen.add
if key is None:
for element in filterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
list(unique_everseen(list_1, key=frozenset))
But it's not clear how to match the removed sublists indices on list_2
Optimal output:
new_list_1 = [[1,1], [1,3], [1,4]]
new_list_2 = ["string_1", "string_2", "string_4"]