How do I get the last element of an iterator? I'm aware that I have to exhaust the iterator and return the last value it produced, so the plain approach would be:
def last(it):
    for value in it:
        pass
    return value
(You might want to catch the NameError and raise something else for empty input, but you get the idea.)
Is there a simpler solution I didn't think of, e. g. using itertools.islice() (it doesn't seem to accept negative indexes, though) or something similar?
 
     
     
     
    