| Store | Cart

reversed(enumerate(x))

From: Ian Kelly <ian....@gmail.com>
Wed, 20 Jul 2016 11:42:02 -0600
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

Recent Messages in this Thread
Ian Kelly Jul 20, 2016 05:42 pm
Chris Angelico Jul 20, 2016 05:46 pm
Steven DAprano Jul 20, 2016 07:13 pm
Chris Angelico Jul 20, 2016 07:47 pm
Brendan Abel Jul 20, 2016 05:54 pm
Ian Kelly Jul 21, 2016 04:25 pm
Random832 Jul 20, 2016 07:16 pm
Ian Kelly Jul 21, 2016 04:13 pm
Michael Selik Jul 21, 2016 01:54 am
Messages in this thread