July 27, 2020
7:24 p.m.
A single-name unpacking assignment can do exactly what you want, albeit with slightly less helpful exception messages: jack, = (p for p in people if p.id == '1234') # note comma after the name jack If no value is yielded by the generator expression, you'll get "ValueError: not enough values to unpack (expected 1, got 0)". If multiple values are yielded, you'll instead get "ValueError: too many values to unpack (expected 1)".