Skip to content

Support enumerate with reversed function #124178

@muddyfish

Description

@muddyfish

Feature or enhancement

Proposal:

Currently, the reversed function doesn't support enumerate objects. This would be useful any time you want to traverse a list backwards whilst keeping indexes.

The implementation would ensure that enumerate objects support reversal only if the iterable argument also supports reversal and len.

lst = ["foo", "bar"]
for i, data in reversed(enumerate(lst)):
    print(i, data)

Currently, what I see being done is something like:

for i in range(len(lst)-1, -1, -1):
  data = lst[i]

Which is less readable.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

Labels

type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions