The first thing that comes to mind is:
>>> l = list('abcdef')
>>> for i in range(len(l)-1, -1, -1):
... item = l[i]
... print(i, item)
...
5 f
4 e
3 d
2 c
1 b
0 a
I tried using the following:
>>> l
['a', 'b', 'c', 'd', 'e', 'f']
>>> for i,ch in reversed(enumerate(l)):
... print(i,ch)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'enumerate' object is not reversible
but apparently 'enumerate' object is not reversible. I can trick it with:
>>> for i,ch in reversed(list(enumerate(l))):
... print(i,ch)
...
5 f
4 e
3 d
2 c
1 b
0 a
but that doesn't feel right - it's a bit cumbersome. Is there a better way to do this? Maybe some inverse_enumerate
hidden is a lib like collections or itertools?