How to remove all items that are in between two duplicates in a list

Question:

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:

    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

Asked By: user20194358

||

Answers:

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: # while `lst` is non-empty
    x, *lst = lst # if lst = [1,2,3], for example, now x = 1 and lst = [2,3]
    output.append(x)
    try: # try finding the x in lst
        lst = lst[lst.index(x)+1:] # if found, reduce the lst (i.e., skip the first lst.index(x)+1 elememts
    except ValueError: # if not found
        pass # do nothing

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.

Answered By: j1-lee

Here’s one way.

from collections import Counter

a = [0, 'x', 2, 3, 'x', 4, 'y', 'y', 6]

# Count the number of occurrence of each unique value
counts = Counter(a)

removing = None
new_list = []
for item in a:
    if removing: 
        if item == removing:
            removing = None
        continue
    if counts[item] > 1:
        removing = item
    new_list.append(item)

print(new_list)

Output:

[0, 'x', 4, 'y', 6]
Answered By: Bill
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.