dict.items() vs dict.iteritems and similar questions

Duncan Booth duncan.booth at invalid.invalid
Thu Mar 15 11:56:46 EDT 2007


aleax at mac.com (Alex Martelli) wrote:

>> Of course this is a little like the Heisenberg uncertainty principle if
>> the iterator has no __len__ attribute - once you know how long it is you
>> no longer have access to the elements. Or did I miss something?
> 
> Right.  However, "return sum(1 for _ in iterator)" may be a handier way
> to express the same desctructive semantics as the last 4 lines here.

I think I'd prefer the barbaric:

   return len(list(iterator))

since at least it is guaranteed to terminate.



More information about the Python-list mailing list