This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: reverse parameter for enumerate()
Type: enhancement Stage: resolved
Components: Build Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, eric.smith, wyz23x2
Priority: normal Keywords:

Created on 2020-02-10 07:35 by wyz23x2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg361670 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-02-10 07:35
Starting from Python 2.3, the handy enumerate() was introduced.
However, I suggest to add a "reverse" parameter:
>>> lis = ['a', 'b', 'c', 'd']
>>> list(enumerate(lis))
[(0,'a'),(1,'b'),(2,'c'),(3,'d')]
>>> list(enumerate(lis,reverse=True)
[('a',0),('b',1),('c',2),('d',3)]
>>>
msg361671 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-02-10 07:37
A typo in the previous comment:
>>> list(enumerate(lis,reverse=True))
[('a',0),('b',1),('c',2),('d',3)]
msg361674 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-02-10 08:09
What is the use case for this? You seem to want `enumerate` to return (item, index) instead of (index, item) when `reverse=True`? You can achieve this yourself easily a custom generator:

>>> def swapped_enumerate(l):
...   for idx, item in enumerate(l):
...     yield item, idx
...
>>> list(swapped_enumerate(lis))
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
msg361676 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-02-10 09:24
You can already do this using existing composable tools, including:

>>> list((item, idx) for idx, item in enumerate(lis))
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
>>>

We won't be adding a parameter to enumerate in order add another way of doing this.

If you really want to pursue this, you should discuss it on the python-ideas mailing list and try to get the idea accepted there. But it really doesn't have any chance of being accepted.
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83777
2020-02-10 09:24:03eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg361676

resolution: rejected
stage: resolved
2020-02-10 08:09:00ammar2setnosy: + ammar2
messages: + msg361674
2020-02-10 07:37:03wyz23x2setmessages: + msg361671
2020-02-10 07:35:43wyz23x2create