How to remove duplicates of class objects from a set () in Python 2.7?

advertisements

Let's define a simple class:

class sample(object):
def __init__(self, key):
    self.key = key

Let's say that a1, a2, a3, a4, a5 are objects of "sample" class (a1 and a5 share the same "key" element):

a1 = sample('test1')
a2 = sample('test2')
a3 = sample('test3')
a4 = sample('test4')
a5 = sample('test1')

Now, if I add these elements to a list this will be the result:

 [<__main__.sample at 0x1189c1e90>,
 <__main__.sample at 0x1189c1b90>,
 <__main__.sample at 0x1189c15d0>,
 <__main__.sample at 0x1189c1dd0>,
 <__main__.sample at 0x1189c16d0>]

But if I convert this list to a "set" object (to remove a1/a5) the result will be:

 {<__main__.sample at 0x1189c15d0>,
 <__main__.sample at 0x1189c16d0>,
 <__main__.sample at 0x1189c1b90>,
 <__main__.sample at 0x1189c1dd0>,
 <__main__.sample at 0x1189c1e90>}

So, the question is: how can I modify the class so that it can set two objects that share the same "key" as the very same object (so I don't have duplicates in the set())? I'm looking for a very simple implementation :)


You must implement your own __hash__ and __eq__ methods to ensure they are considered equal when constructing the set:

class Sample(object):
    def __init__(self, key):
        self.key = key

    def __hash__(self):
        return hash(self.key)

    def __eq__(self, other):
        return self.key == other.key