neuops
neuops

Reputation: 352

Removing earlier duplicates from a list and keeping order

I want to define a function that takes a list as an argument and removes all duplicates from the list except the last one.

For example: remove_duplicates([3,4,4,3,6,3]) should be [4,6,3]. The other post answers do not solve this one.

The function is removing each element if it exists later in the list. This is my code:

def remove(y):
    for x in y:
        if y.count(x) > 1:
            y.remove(x)
            
    return y

and for this list: [1,2,1,2,1,2,3] I am getting this output: [2,1,2,3]. The real output should be [1,2,3]. Where am I going wrong and how do I fix it?

Upvotes: 1

Views: 78

Answers (2)

wjandrea
wjandrea

Reputation: 32944

The other post does actually answer the question, but there's an extra step: reverse the input then reverse the output. You could use reversed to do this, with an OrderedDict:

from collections import OrderedDict

def remove_earlier_duplicates(sequence):
    d = OrderedDict.fromkeys(reversed(sequence))
    return reversed(d)

The output is a reversed iterator object for greater flexibility, but you can easily convert it to a list.

>>> list(remove_earlier_duplicates([3,4,4,3,6,3]))
[4, 6, 3]
>>> list(remove_earlier_duplicates([1,2,1,2,1,2,3]))
[1, 2, 3]

BTW, your remove function doesn't work because you're changing the size of the list as you're iterating over it, meaning certain items get skipped.

Upvotes: 1

neuops
neuops

Reputation: 352

I found this way to do after a bit of research. @wjandrea provided me with the fromkeys method idea and helped me out a lot.

def retain_order(arr): 
    return list(dict.fromkeys(arr[::-1]))[::-1]

Upvotes: 1

Related Questions