1118
The reversed builtin function is handy:
for item in reversed(sequence):
The documentation for reversed explains its limitations.
For the cases where I have to walk a sequence in reverse along with the index (e.g. for in-place modifications changing the sequence length), I have this function defined an my codeutil module:
from six.moves import zip as izip, range as xrange
def reversed_enumerate(sequence):
return izip(
reversed(xrange(len(sequence))),
reversed(sequence),
)
This one avoids creating a copy of the sequence. Obviously, the reversed limitations still apply.