You could create your own generator that wraps enumerate def reverse_enumerate(iterable): for i, val in enumerate(reversed(iterable)): yield len(iterable) - 1 - i, val
for i, val in reverse_enumerate(x): ... On Wed, Jul 20, 2016 at 10:42 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > I had occasion to write something like this: > > for i, n in reversed(enumerate(x)): pass > > Of course this fails with "TypeError: argument to reversed() must be a > sequence". I ended up using this instead: > > for i, n in zip(reversed(range(len(x))), reversed(x)): pass > > This works but is extraordinarily ugly and not terribly clear. I think > it's less confusing however than: > > for i, n in zip(range(len(x)-1, -1, -1), reversed(n)): pass > > How would you write this? > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list