For lists one can use slicing my_list[-10:] to get the last (up to) 10 elements
I want to do the same with itertools.islice. In this case I have a collections.deque.
Is there a more efficient way of doing this than the following?
from collections import deque
import itertools
my_deque = deque()
my_list = list()
for i in range(100):
my_deque.append(i)
my_list.append(i)
And then we do the slicing:
start = max(0, len(my_deque) - 10)
for i in list(itertools.islice(my_deque, start, None)):
pass
My timings:
deque: 1000000 loops, best of 3: 962 ns per loop
list slicing: 10000000 loops, best of 3: 95.9 ns per loop