(1) Shouldn't map() and reduce() be treated similarly?
(2) A possibly even more efficient way of doing this, which might work
for 'for' loops as well, would be to never ask for the length at all
-- simply keep asking for the next element until it raises IndexError.
This would in some cases require extra code (e.g. like what you
discovered about needing to use additem) but sounds like the most
general solution and even somewhat elegant...
Enter dream mode...
If I have an object representing a non-linear array (like a
dictionary) then it could return as its keys() an indefinite-length
sequence which generated the keys on the fly. You could then write
for key in x.keys():
And we would have introduced generators in the language without any
--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>