Finding an item based on computed value
Recently, at work I had to implement some code which had the pattern:
found = None
for x in foo:
if bar(x) == baz:
found = x
One could do this in a more Pythonic way as:
try:
found = next(x for x in foo if bar(x) == baz)
except StopIteration, e:
found = None