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 remove all the items that are in between two duplicates in a list

How do I write a program that will remove all the items that are in between two duplicates in a list and it will also remove the second duplicate.

For example,
a = [ (0,0) , (1,0) , (2,0) , (3,0) , (1,0) ]
In the list a, we see that (1,0) occurs more than once in the list. Thus I want to remove all the items in between the 2 duplicates and I want to remove the second occurrence of (1,0).
Thus, in this example, I want to remove (2,0),(3,0) and the second occurrence of (1,0).
now my list would look like this : a = [(0,0),(1,0)]
I was able to do this however the problem occurs when I have more than 1 duplicates in my list.
For example,
b = [ (0,0) , (1,0) , (2,0) , (3,0) , (1,0) , (5,0) , (6,0) , (7,0) , (8,0) , (5,0), (9,0) , (10,0) ]
In this example, we see that that I have 2 items that are duplicates. I have (1,0) and I have (5,0). Thus, I want to remove all the items between (1,0) and the second occurrence of (1,0) including its second occurrence and I want to remove all the items between (5,0) and the second occurrence of (5,0). In the end, my list should look like this :
b = [ (0,0) , (1,0) ,(5,0) , (9,0) ]

This is what I have thus far:

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

    a = [ (0,0) , (1,0) , (2,0) , (3,0) , (1,0) ]
indexes_of_duplicates = []
for i,j in enumerate(a):
    if a.count(j) > 1 :
        indexes_of_duplicates.append(i)
for k in range(indexes_of_duplicates[0]+1,indexes_of_duplicates[1]+1):
    a.pop(indexes_of_duplicates[0]+1)
print(a)  

Output : [(0, 0), (1, 0)]
as you can see, this code would only work if I have only 1 duplicate in my list, but I have no idea how to do it if I have more than one duplicate.
PS : I can’t obtain a list with overlaps like this [(1, 0), (2, 0), (3, 0), (1, 0), (2, 0)]. thus, you can ignore lists of this kind

>Solution :

Here’s one way to do that by using index:

lst = [(0,0), (1,0), (2,0), (3,0), (1,0), (5,0), (6,0), (7,0), (8,0), (5,0), (9,0), (10,0)]

output = []

while lst:
    x, *lst = lst
    output.append(x)
    try:
        lst = lst[lst.index(x)+1:]
    except ValueError:
        pass

print(output) # [(0, 0), (1, 0), (5, 0), (9, 0), (10, 0)]

Note that lst will be exhausted. If you want to preserve it, you can copy it beforehand.

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