Iterator excluding the last element

What is a pythonic way to implement an iterator that excludes the last element without knowing its length?

Example:

>>> list(one_behind(iter(range(10)))
... [0, 1, 2, 3, 4, 5, 6, 7, 8]

>>> iter_ = one_behind(iter((3, 2, 1)))
>>> next(iter_)
... 3
>>> next(iter_)
... 2
>>> next(iter_)
... StopIteration

      

A simple approach would be to use a loop and keep the previous value, but I would like something a little shorter.

Implementing links using a loop:

def one_behind(iter_):
    prev = None
    for i, x in enumerate(iter_):
        if i > 0:
            yield prev
        prev = x

      

+3


source to share


3 answers


Using itertools.tee

:

import itertools

def behind(it):
    # it = iter(it)  # to handle non-iterator iterable.
    i1, i2 = itertools.tee(it)
    next(i1)
    return (next(i2) for x in i1)

      



using:

>>> list(behind(iter(range(3))))
[0, 1]

      

+4


source


Negatively simpler than the reference function:



def lag(iter):
    previous_item = next(iter)
    for item in iter:
        yield previous_item
        previous_item = item

      

+2


source


Code:

def one_behind(iter):
    q = collections.deque()
    q.append(next(iter))
    while True:
        q.append(next(iter))
        yield q.popleft()

      

Runtime:

>>> iterator = iter(range(4))
>>> list(one_behind(iterator))
[0, 1, 2]

      

0


source







All Articles