reversed(enumerate(x))

Ian Kelly ian.g.kelly at gmail.com
Thu Jul 21 12:13:51 EDT 2016


On Wed, Jul 20, 2016 at 1:16 PM, Random832 <random832 at fastmail.com> wrote:
> On Wed, Jul 20, 2016, at 13:42, Ian Kelly wrote:
>> I had occasion to write something like this:
>>
>>     for i, n in reversed(enumerate(x)): pass
>>
>> How would you write this?
>
> I'd write my own version of enumerate with a step argument, and call
> enumerate(reversed(x), start=len(x), step=-1)

Makes it a little too easy to commit an off-by-one error, I think. As
you did here.



More information about the Python-list mailing list