len() on mutables vs. immutables

Demian Brecht demianbrecht at gmail.com
Thu Oct 18 14:42:48 EDT 2012


On 10/18/2012 11:29 AM, Terry Reedy wrote:> Or the length could be the 
difference of two pointers -- address of the
 > first empty slot minus address of first item.

That would assume contiguous blocks of memory, which I would find to be 
rather dangerous (of an assumption that is) in most dynamic cases 
(obviously totally depends on implementation details).

 > str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and
 > ranges should all return len in O(1) time. That includes the possibility
 > of a subtraction as indicated above.

Awesome. Pretty much what I figured. Of course, I'll have to dig around 
the source just to confirm this with my own eyes (more just curiosity 
than anything), so if you know whereabouts to look, it would be most 
helpful :)



More information about the Python-list mailing list