Pre-PEP: reverse iteration methods

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Wed Sep 24 12:44:45 EDT 2003


On Wed, 24 Sep 2003 00:30:31 GMT, rumours say that "Raymond Hettinger"
<vze4rx4y at verizon.net> might have written:

>Abstract
>========
>
>This proposal is to extend the API of several sequence types
>to include methods for iterating over the sequence in reverse.

-0

I believe this should be a function in itertools.

For indexable sequences, a little modification of itertools.islice would
do the trick, since it can't at the moment; it's designed for forward
iteration.

Helpful error messages:
>>> itertools.islice(lst, len(lst)-1, -1, -1)
ValueError: Stop argument must be an integer or None.
>>> itertools.islice(lst, len(lst)-1, None, -1)
ValueError: Step must be one or larger for islice().

For non-indexable sequences (eg a file object), things get hairier, but
given enough memory, it's not hard.
-- 
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.




More information about the Python-list mailing list