Recent items

Write last_n(items, n) that returns a list of the last n items of items, in their original order, using a deque with maxlen. If there are fewer than n items, return all of them.

last_n(["apple", "bread", "milk", "eggs"], 2)  ->  ["milk", "eggs"]
last_n(["apple"], 3)                           ->  ["apple"]
from collections import deque

def last_n(items, n):
    recent = deque(maxlen=n)
    for item in items:
        ...
    return list(recent)