[Python-ideas] Previous item from iterator.

Steven D'Aprano steve at pearwood.info
Thu Apr 16 01:03:43 CEST 2015


On Wed, Apr 15, 2015 at 10:25:58PM +0000, Andrew Barnert wrote:

> The paradigm examples of bidirectional iteration are linked-list-like 
> and tree-like iteration. In particular, almost all kinds of sorted 
> containers can be iterated in sorted order bidirectionally, but not 
> randomly.

In Python terms though, if you think you want a linked-list, you 
probably actually want a regular list. I'm not so sure about trees, but 
the lack of any trees in the std lib is probably a hint that a dict 
makes a good replacement.

[...]
> There aren't too many fundamental algorithms that require 
> bidirectional iteration but not random-access iteration, or that can 
> be more efficient with bidirectional than with forward, but there are 
> some.

Are any of them unable to be efficiently reimplemented in terms of 
random access lists? What does bidirectional iteration give us that 
sequences don't?

The rest of your post is interesting, but I don't see the relevance to 
*Python*. Bidirectional iteration isn't being proposed in a vacuum, it 
has to compete with existing alternatives such as lists and dicts.


-- 
Steve


More information about the Python-ideas mailing list