[issue33040] Make itertools.islice supports negative values for start and stop arguments for sized iterable object

TitanSnow report at bugs.python.org
Mon Mar 12 05:20:43 EDT 2018


TitanSnow <tttnns1024 at gmail.com> added the comment:

Now I have thought about it and realized that
it's not suitable for islice.
But there's still a common use case to
drop some elements from the beginning or
ending of a iterable, which is also a main
reason why I wanted islice to support
negative values of start and stop.
So how about adding two functions
``drop_first`` and ``drop_last``
to do such things. They can be implemented
like this, in which way they support
arbitrary iterators::

    def drop_first(iterable, n=1):
        for _ in range(n):
            next(iterable)
        for e in iterable:
            yield e

    def drop_last(iterable, n=1):
        dq = deque()
        for _ in range(n):
            dq.append(next(iterable))
        for e in iterable:
            dq.append(e)
            yield dq.popleft()

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33040>
_______________________________________


More information about the Python-bugs-list mailing list