Get the last item from an iterator in Python

Use a deque of size 1.

from collections import deque


def last(iterator):
    q = deque(iterator, maxlen=1)
    return q.pop() if q else None