I have the following list:
list1 =[('a','b'),('c','d'),('e','f'),('g','h'),('b','a'),('e','d'),('e','g'),('h','g')]
I wish to append the indices of reversed duplicates from this list. For example:
('a','b') == ('b','a')
('g','h') == ('h','g')
I tried
exec =[]
for i,x in enumerate(list1):
z=x[::-1]
if z in list1:
exec.append(i)
list1.remove(z)
I got:
exec
[0, 3]
Which is correct. However, this is very inefficient when running on a 10 million elements of list. I know that I can directly remove the reversed duplicates by:
data = list({tuple(sorted(item)) for item in list1})
But I only want to identify the indices of reversed duplicates here. Is there a better way to do it? Thanks in advance.
>Solution :
You can use tuples as a key of a dictionary, so create a dictionary where the sorted tuples are keys and the values are the first index where that tuple occurs in your list. Then, when you encounter a tuple that’s already in the dictionary, you can add the index to your output list:
indices_dict = {}
dup_indices = []
for index, item in enumerate(list1):
s_item = tuple(sorted(item))
if s_item in indices_dict:
dup_indices.append(indices_dict[s_item])
else:
indices_dict[s_item] = index
Which gives
dup_indices = [0, 3]