Considering the following class :
class MyClass():
def __init__(self,attr):
self.attr=attr
and a list of MyClass objects :
myList=[MyClass(1),MyClass(2),MyClass(3)]
The attribute values are unique. To get the reference of the object with a specific value (for example : 2), I used
[i for i in myList if i.attr==2][0]
or
myList[[i.attr for i in myList].index(2)]
What is the best way to do that and is there a better solution ? (If I may, as I am a python beginner, answer with the pros and the cons of different solutions would be appreciated). (Rq : this example is very simple, list could be larger and objects more complex)
Thanks for answer.