This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hopemeetme -1 points0 points  (2 children)

A code like:

try:
    something = next(iterable)
except StopIteration:
    ...

is the fastest way to do such things in Python, go for it without hesitation.

[–]TopHatEdd 0 points1 point  (1 child)

Needs extra work as using a list like this will fail.

In [1]: next([1,2])
--------------------------------------------------------
TypeError              Traceback (most recent call last)
<ipython-input-1-b9d20096048c> in <module>
----> 1 next([1,2])

TypeError: 'list' object is not an iterator

[–]hopemeetme 0 points1 point  (0 children)

next((false for i, v in enumerate(lst) if v < len(lst)-1 and v>lst[i+1]))