Damn Exceptions!
Damn Exceptions!
I love thisAlex Martelli's answer. However, since next()
raise a StopIteration
exception when there are no items,
i would use the following snippet to avoid an exception:
a = []
item = next((x for x in a), None)
For example,
a = []
item = next(x for x in a)
Will raise a StopIteration
exception;
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration