I need to load a list of database row objects into memory, and then grab one of those rows by its unique ID. Is there a clean, pythonic way of finding an single object from a list by an attribute value? Or do I just loop and compare?
3 Answers
If you do this it only gives the very first match, instead of comparing the whole list: find first sequence item that matches a criterion.
If you do something like this, you don't have to catch the exception but get None instead:
item = next((i for i in items if i == 'value'), None)