Text of discuss.python.org › t › backward-enumerator › 74480 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Backward enumerator - Ideas - Discussions on Python.org

Backward enumerator Ideas zhangyx (Yuxuan Zhang) December 16, 2024, 9:53pm 1 Currently python provides enumerate(Iterable) for forward enumeration. The API returns a generator of tuples of index and item. However, there lacks a builtin function to enumerate from the back of a Sequence (Iterable plus len and getitem). Currently the most popular solution on StackOverflow is reversed(list(enumurate(...))) (reference to this post ).

This consumes the entire generator and stores them as a list before it can be reversed. I propose providing an additional builtin function enumerate_back or add an optional flag enumerate(..., reversed=True) to support reversed enumeration of Sequence objects. P.S. For the flag version, the input type should be narrowed from Iterable[T] to Sequence[T] . Here is a demo python implementation of enumerate_back : from typing import TypeVar, Sequence, Generator, Any T = TypeVar("T") def enumerate_back(seq: Sequence[T]) -> Generator[tuple[int, T], Any, None]: if not hasattr(seq, "__len__") or not hasattr(seq, "__getitem__"): raise TypeError(f"{seq} does not support sequencing") indexes = range(len(seq) - 1, -1, -1) items = reversed(seq) yield from zip(indexes, items) Given that __getitem__() may incur reading from file or even a remote fetch, the proposed API will be more memory efficient than the solution from the aforementioned SO post which consumes everything before reversing.

My own use case (ROS2) import rclpy from rclpy.node import Node from collections import deque class TaskScheduler(Node): def __init__(self, maxlen: int = 10): self.task_queue = deque[tuple[float, callable]](maxlen=maxlen) # Irrelevant code omitted ... def schedule_task(self, ddl: float, task: callable): while len(self.task_queue) >= self.task_queue.maxlen: # No timeout (wait forever) - we need at least one task to be executed rclpy.spin_once(self) # Insert the task into task queue in ascending order # Searching from back because the new task is likely to be the last for idx, (ts, _) in enumerate_back(self.task_queue): if ts < ddl: self.task_queue.insert(idx + 1, (ddl, task)) break else: self.task_queue.appendleft((ddl, task)) EDIT: inspired by an earlier GitHub issue shared by @Stefan2 (thanks!), I came up with another alternative approach of implementing this feature.

IMO this will be the most ideal one among all proposed solutions: from typing import Iterable import builtins class enumerate(builtins.enumerate): # EDIT2: make `isinstance(..., collections.abc.Reversible)` happy. def __new__(cls, obj, **kwargs): if hasattr(obj, "__len__") and hasattr(obj, "__reversed__"): return cls.reversible(obj, **kwargs) else: return builtins.enumerate(obj, **kwargs) class reversible(builtins.enumerate): def __init__(self, iterable: Iterable, start: int=0): self.iterable = iterable self.start = start def __reversed__(self): indexes = range(len(self.iterable) - 1, self.start - 1, -1) items = reversed(self.iterable) return zip(indexes, items) Usage: # Works on all objects that supports len() and reversed() reversed(enumerate(range(10))) # Throws TypeError for objects that do not support these methods # (e.g. a Generator) reversed(enumerate((i for i in range(10)))) # TypeError 2 Likes jamestwebber (James Webber) December 16, 2024, 9:54pm 2 Why not ((len(seq) - i, e) for i, e in enumerate(reversed(seq))) ? edit: Only took me three tries 1 Like zhangyx (Yuxuan Zhang) December 16, 2024, 9:56pm 3 Because the actual index needs to be computed in each iteration jamestwebber (James Webber) December 16, 2024, 9:56pm 4 That’s true for enumerate too? zhangyx (Yuxuan Zhang) December 16, 2024, 9:57pm 5 Well it should incur fewer numeric operation (for each iteration). jamestwebber (James Webber) December 16, 2024, 10:00pm 6 Sure, a tiny bit.

Does that micro-optimization justify adding this code, though? I’m skeptical. Oh, another option to avoid this: zip(itertools.count(), reversed(seq)) Is that better? zhangyx (Yuxuan Zhang) December 16, 2024, 10:08pm 7 I am running timeit tests for them. The point is to provide a standardized way of doing this so you do not have to hand write it every time and suspect its correctness when debugging. The same claim that you made applies to enumerate() itself - everyone can implement their own in a few lines of python.

But yet it exists and has been used widely. 1 Like Stefan2 (Stefan Pochmann) December 16, 2024, 10:15pm 8 Because all indices are 1 too high. 1 Like zhangyx (Yuxuan Zhang) December 16, 2024, 10:16pm 9 This is equivalent to my demo implementation after a tiny modification, but more efficient (maybe due to lack of yield from and lack of type checking?) zip(itertools.count(len(seq) - 1, -1), reversed(seq)) Test Code: def back_enumerate(seq): ... # See above def comparison1(seq): return ((len(seq) - i, e) for i, e in enumerate(reversed(seq))) def comparison2(seq): from itertools import count return zip(count(len(seq) - 1, -1), reversed(seq)) from timeit import timeit seq = range(10000) print("back_enumerate", timeit('list(back_enumerate(seq))', globals=globals(), number=1000)) print("comparison1 ", timeit('list(comparison1(seq))', globals=globals(), number=1000)) print("comparison2 ", timeit('list(comparison2(seq))', globals=globals(), number=1000)) Timeit results: ~/playground $ python3 back_enumerate.py back_enumerate 0.557957124983659 comparison1 0.8546551250037737 comparison2 0.44767408299958333 # Best 1 Like Stefan2 (Stefan Pochmann) December 16, 2024, 10:20pm 10 Yeah, that needlessly wraps the zip iterator in a generator, slowing it down.

It’s an anti-pattern. 1 Like zhangyx (Yuxuan Zhang) December 16, 2024, 10:29pm 11 I’ve modified the original implementation and added the SO solution for comparison. My modified original implementation now wins constantly. Test code from typing import TypeVar, Sequence T = TypeVar("T") def back_enumerate(seq: Sequence[T]): if not hasattr(seq, "__len__") or not hasattr(seq, "__getitem__"): raise TypeError(f"{seq} does not support sequencing") indexes = range(len(seq) - 1, -1, -1) items = reversed(seq) return zip(indexes, items) def no_type_check(seq: Sequence[T]): indexes = range(len(seq) - 1, -1, -1) items = reversed(seq) return zip(indexes, items) def stack_overflow(seq: Sequence[T]): return reversed(list(enumerate(seq))) def comparison1(seq: Sequence[T]): return ((len(seq) - i, e) for i, e in enumerate(reversed(seq))) def comparison2(seq: Sequence[T]): from itertools import count return zip(count(len(seq) - 1, -1), reversed(seq)) from timeit import timeit seq = range(10000) def test(stmt: str): t = timeit(stmt, globals=globals(), number=10000) return f"{t:.8f}" print("back_enumerate", test('list(back_enumerate(seq))')) print("no_type_check ", test('list(no_type_check(seq))')) print("stack_overflow", test('list(stack_overflow(seq))')) print("comparison1 ", test('list(comparison1(seq))')) print("comparison2 ", test('list(comparison2(seq))')) timeit Results: back_enumerate 4.11149596 no_type_check 4.09617529 # back_enumerate without type checking stack_overflow 4.35428938 comparison1 8.54001383 comparison2 4.47707404 Stefan2 (Stefan Pochmann) December 16, 2024, 10:31pm 12 Also note this: Stefan2 (Stefan Pochmann) December 16, 2024, 10:35pm 13 Could you please include the needed imports? pf_moore (Paul Moore) December 16, 2024, 10:35pm 14 I don’t think this is needed often enough to justify adding to the stdlib.

It might be welcome in the more_itertools library, though, or maybe as a recipe in the itertools docs. 3 Likes zhangyx (Yuxuan Zhang) December 16, 2024, 10:38pm 15 Sry, fixed. Interesting. How about exposing enumerate().__reversed__() when the object being enumerated supports __len__ and __reversed__ ? 1 Like zhangyx (Yuxuan Zhang) December 16, 2024, 11:08pm 16 Coming back with a demo implementation (I’ll update my OP and add this one): from typing import Iterable import builtins class enumerate(builtins.enumerate): def __init__(self, iterable: Iterable, start: int=0): self.iter

The rest of this page is on the live site.