Lets say I have a class
class Foo:
def __init__(self, item1, item2):
self.item1 = item1
self.item2 = item2
And a objects list of that class
object1 = Foo (1,2)
object2 = Foo (1,2)
object3 = Foo (1,3)
objectlist = [object1, object3]
I would like to know whether object2 with same items is in the list of objectlist and after that I would like to get the index of it. In this case 0 index.
I can do this by
def __eq__ (self, other):
return (self.item1 == other.item1) and (self.item2 == other.item2)
And a for loop. Since I could be able to check every index one by one, and got the index if it equals to that object. But can I do it with more elagant way?