Title: reverse parameter for enumerate()
Type: enhancement Stage: resolved
Components: Build Versions: Python 3.9
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 2020-02-10 09:24 by eric.smith. 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))
>>> list(enumerate(lis,reverse=True)
msg361671 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-02-10 07:37
A typo in the previous comment:
>>> list(enumerate(lis,reverse=True))
msg361674 - (view) Author: Ammar Askar (ammar2) * (Python triager) 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.
Date User Action Args
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