Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the indices of reversed duplicates in a list

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading