iterators and views of lists

Brendan Miller catphive at catphive.net
Wed Dec 16 13:57:45 EST 2009


On Wed, Dec 16, 2009 at 4:16 AM, Paul Rudin <paul.nospam at rudin.co.uk> wrote:
> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>
>
>> I'm sympathetic to your request for list views. I've often wanted some
>> way to cleanly and neatly do this:
>>
>> for item in seq[1:]:
>>     process(item)
>>
>> without making an unnecessary copy of almost all of seq.
>>
>
> I don't know how it's implemented - but presumably itertools.islice
> could provide what you're asking for?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

itertools.islice returns an iterator. My main point is that in python
iterators are weak and can't be used to write many types of
algorithms. They only go in one direction and they can't write to the
collection.

Another poster mentioned a stream library that is also iterator based.
Basically I'm saying that anything with iterators is pretty limited
because of the interface they present. See section 6.5 here for the
iterator protocol:

http://docs.python.org/library/stdtypes.html

Basically this is only useful for writing for loops or map/reduce
operations. However, python's primary datastructures, the dynamic
array (list) and hashtable (dictionary) are more powerful than that.



More information about the Python-list mailing list