Message313517
``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. |
|
Date |
User |
Action |
Args |
2018-03-10 09:18:47 | tttnns | set | recipients:
+ tttnns |
2018-03-10 09:18:46 | tttnns | set | messageid: <1520673526.96.0.467229070634.issue33040@psf.upfronthosting.co.za> |
2018-03-10 09:18:46 | tttnns | link | issue33040 messages |
2018-03-10 09:18:46 | tttnns | create | |
|