iterators and views of lists

Brendan Miller catphive at catphive.net
Tue Dec 15 22:39:29 EST 2009


I was trying to reimplement some of the c++ library of generic
algorithms in c++ in python, but I was finding that this is
problematic to do this in a generic way because there isn't any
equivalent of c++'s forward iterators, random access iterators, etc.
i.e. all python iterators are just input iterators that can't mutate
the sequence they iterate over nor move backwards or by an arbitrary
offset.

I'm wondering if anyone has done work towards creating more powerful
iterators for python, or creating some more pythonic equivalent.

In particular, I was thinking that slices are almost equivalent to a
range of random access iterators, except that they create an
unnecessary copy. If there was a version of slice that defined a
mutable view over the original list, you'd have something equivalent
to t a pair of random access iterators.

For instance, if you wanted to reverse a segment of a list in place
you would be able to do:

list = [1,2,3,4]

#pretend this is some variant of slice that produces views instead
#obviously this couldn't use the same syntax, and might be a method instead.
view = list[1:]
view.reverse()

print list
[1,4,3,2]

This doesn't really handle forward and bidirectional iterators...
which I guess would be good for algorithms that operator over disk
base datastructures...

Anyone else had similar thoughts?

Brendan



More information about the Python-list mailing list