Q:

Select an element that satisfies a condition

I would like to select a specific element that satisfies a condition from a (very long) list. In my specific case each item in the list is a class and the condition I want is, for example, item.id == 1. 
I started with
[item for item in collection if item.id == 1]

but I don't want to traverse the whole list because I am sure there is only one element that satisfied the condition.
The other option is
def check(collection):
    for item in collection:
        if item.id == 1:
            return item

Is there a more efficient way? Does Python 3 have a build in function for that?
Another option could be to preprocess the list in order to obtain a dictionary, e.g.
{item.id : item for item in collection}

A:

You can use a generator expression and the next() function:
item = next((item for item in collection if item.id == 1), None)

This'll find the first matching element in the collection, or return None if there is no match. It'll not iterate and evaluate any more than is strictly required to find that one element.
However, if you do need to do this a lot, creating the mapping just once then querying that each time is going to be more efficient:
mapping = {item.id: item for item in collection}
item = mapping.get(1)

as that'll limit looping to just once, then use the O(1) lookup performance. It is a memory-vs-speed trade-off.

