serial iteration over several lists

Martin DeMello martindemello at yahoo.com
Mon Aug 23 16:30:14 CEST 2004


Jeffrey Froman <Jeffrey at fro.man> wrote:
> Martin DeMello wrote:
> 
> > Thanks - that looks like an excellent starting point. Has there been any
> > work done on bidirectional iterators?
> 
> You could emulate one by maintaining a counter for the current index into
> the imaginary flattened list, and iterating up to that index with each
> prev() call and just past it with each next(). This of course requires that
> you basically start from the beginning of your nested list each time
> though, and I'm not sure if it would be more or less efficient than
> indexing into a pre-flattened list in memory.

I ended up just maintaining a few list indexes, and writing code to wrap
them around properly - turned out to be the simplest way since
bidirectionality was more important than iterator-like support.

> One more suggestion -- if you use a recursive generator for your iteration,
> you can then use it with a list nested to arbitrary depth, something like:
> 
> #############################################
> 
> def deep_iter(nested_list):
>     for x in nested_list:
>         if isinstance(x, list):
>             for n in deep_iter(x):
>                 yield n
>         else:
>             yield x
> 
> #############################################

I thought of that, but again it proved nigh impossible to backtrack
cleanly over. The other idea I had was to maintain two iterators, one
going forwards and one going backwards, and reinitialize them as needed.
For example, if I wanted to do that over the list [1,2,3,4,5,6,7,8,9]
I'd set forward to 1 and reverse to 9, then iterate forward as long as
next was called, then when prev was called, reinitialize reverse to
wherever the index had reached and call its next method.

martin



More information about the Python-list mailing list