Cleanest way to get last item from Python iterator

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2010-01-26T10:53:07Z Indexed on 2010/05/27 18:11 UTC
Read the original article Hit count: 206

Filed under:
|

What's the best way of getting the last item from an iterator in Python 2.6? For example, say

my_iter = iter(range(5))

What is the shortest-code / cleanest way of getting 4 from my_iter?

I could do this, but it doesn't seem very efficient:

[x for x in my_iter][-1]

© Stack Overflow or respective owner

Related posts about python

Related posts about iterator