reversed(enumerate(x))

Ian Kelly ian.g.kelly at gmail.com
Wed Jul 20 13:42:02 EDT 2016


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?



More information about the Python-list mailing list