len() on mutables vs. immutables
ian.g.kelly at gmail.com
Thu Oct 18 21:38:33 CEST 2012
On Thu, Oct 18, 2012 at 1:18 PM, Prasad, Ramit
<ramit.prasad at jpmorgan.com> wrote:
> Why does pointer arithmetic work for dicts? I would think the position
> of a value would be based on the hash of the key and thus "random" for
> the context of this conversation.
It doesn't. len() on CPython dicts is O(1) because the dict keeps
track of how many items it contains. It needs to do this anyway so
that it can determine when to grow the internal hash table.
More information about the Python-list