The cleanest way to get the last item from a Python iterator

What is the best way to get the last item from an iterator in Python 2.6? For example, let's say

my_iter = iter(range(5)) 

What is the shortest code / cleanest way to get 4 from my_iter ?

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

 [x for x in my_iter][-1] 
+93
python iterator
Jan 26 '10 at 10:53 on
source share
13 answers
 item = defaultvalue for item in my_iter: pass 
+81
Jan 26 '10 at
source share

Use a deque size 1.

 from collections import deque #aa is an interator aa = iter('apple') dd = deque(aa, maxlen=1) last_element = dd.pop() 
+52
Jul 02 2018-10-23T00:
source share

If you are using Python 3.x:

 *_, last = iterator # for a better understanding check PEP 448 print(last) 

if you are using Python 2.7:

 last = next(iterator) for last in iterator: continue print last 


NB:

Usually the solution presented above is what you need for ordinary cases, but if you are dealing with a large amount of data, it is more efficient to use a deque size 1. ( source )

 from collections import deque #aa is an interator aa = iter('apple') dd = deque(aa, maxlen=1) last_element = dd.pop() 
+42
Jan 12 '18 at 19:07
source share

It might be worth using __reversed__ if available

 if hasattr(my_iter,'__reversed__'): last = next(reversed(my_iter)) else: for last in my_iter: pass 
+32
Feb 05 '10 at 23:01
source share

Easier than:

 max(enumerate(the_iter))[1] 
+21
Jun 06 2018-11-11T00:
source share

It is unlikely to be faster than an empty loop due to lambda, but perhaps it will give someone else an idea

 reduce(lambda x,y:y,my_iter) 

If iter is empty, TypeError raised

+19
Jan 26 '10 at 11:59
source share

This

 list( the_iter )[-1] 

If the iteration length is truly epic - so long that materializing the list runs out of memory - then you really need to rethink the design.

+9
Jan 26
source share

I would use reversed , except that it only accepts sequences instead of iterators, which seems pretty arbitrary.

In any case, you have to go through the entire iterator. At maximum efficiency, if you no longer need an iterator, you can simply destroy all the values:

 for last in my_iter: pass # last is now the last item 

I think this is not an optimal solution.

+4
Jan 26 '10 at 10:57
source share

See this code for something like this:

http://excamera.com/sphinx/article-islast.html

you can use it to get the last item with:

 [(last, e) for (last, e) in islast(the_iter) if last] 
+1
Jan 12 '11 at 18:08
source share

I would just use next(reversed(myiter))

+1
Oct 24 '17 at 18:48
source share

Toolz library provides a good solution:

 from toolz.itertoolz import last last(values) 

But adding a non-core dependency may not be worth using it only in this case.

+1
Nov 27 '18 at 20:00
source share

The question is to get the last element of the iterator, but if your iterator was created by applying conditions to the sequence, then the inverse can be used to search for the "first" inverse sequence, only looking at the necessary elements, applying Back to the sequence itself.

Invented example

 >>> seq = list(range(10)) >>> last_even = next(_ for _ in reversed(seq) if _ % 2 == 0) >>> last_even 8 
0
May 17 '19 at 18:19
source share

The question is incorrect and can only lead to a complex and ineffective answer. To get an iterator, you, of course, start with something iterative, which in most cases will offer a more direct way to access the last element.

Once you create an iterator from an iterable, you get stuck in the elements, because that is the only thing that provides an iteration.

Thus, the most effective and understandable way is not to create an iterator in the first place, but to use your own methods of accessing an iterable.

-8
Jun 29 '10 at 2:37 a.m.
source share



All Articles