New submission from TitanSnow <>:

``islice()`` does not support negative values for start or stop,
which does not matter for plain iterators.
However, for some cases, we have a sized iterable object
which is not subscriptable, using ``islice()`` makes
code ugly::

    d = OrderedDict()
    for i in range(10): d[i] = i
    dv = d.keys()
    # dv is a KeysView which is a sized iterable

    # now I wanna get a slice of dv which does not contain the last element
    islice(dv, len(dv) - 1)

As it shows, I have to use ``len(dv)`` to get its length.

For sized iterable objects, ``islice()`` could
support negative values for start or stop.
In this way, the above code can be written like this::

    islice(dv, -1)

For iterable objects which is not sized,
it could still be not supported::

    islice(iter(range(10)), -1)

raises a ValueError as its original behavior.

components: Library (Lib)
files: islice.patch
keywords: patch
messages: 313517
nosy: tttnns
priority: normal
severity: normal
status: open
title: Make itertools.islice supports negative values for start and stop 
arguments for sized iterable object
type: enhancement
Added file:

Python tracker <>
Python-bugs-list mailing list

Reply via email to