removing copies from a class

4042 views python

For context: Users can add items to a list. I want to remove all identical submissions. Is there a more efficient way to do this:?

def removeCopies(self):
    i = 0
    while i != self.size:
        number = self.contents.count(self.contents[i])
        if number != 1:
            for j in range(1,number):
                self.delete(self.contents.index(self.contents[i],i + 1))
        i += 1

answered question

Why not use a set to begin with?

1 Answer


The most efficient way I can think of to delete duplicates is

self.contents = list(set(self.contents))

This will not preserve the order of your items, but at least you won't end up with an O(n^3) algorithm.

posted this

Have an answer?


Please login first before posting an answer.