Return list element by the value of one of its attributes
<p>There is a list of objects</p>

<pre><code>l = [obj1, obj2, obj3]
</code></pre>

<p>Each <code>obj</code> is an object of a class and has an <code>id</code> attribute. </p>

<p>How can I return an <code>obj</code> from the list by its <code>id</code>?</p>

<p>P.S. <code>id</code>s are unique. and it is guaranteed that the list contains an object with the requested <code>id</code></p>

<p>Assuming the <code>id</code> is a hashable object, like a string, you should be using a dictionary, not a list.</p>

<pre><code>l = [obj1, obj2, obj3]
d = {o.id:o for o in l}
</code></pre>

<p>You can then retrieve objects with their keys, e.g. <code>d['ID_39A']</code>.</p>

python python-3.x