Why are tuples immutable?

Roy Smith roy at panix.com
Thu Dec 16 18:50:12 CET 2004

Max M  <maxm at mxm.dk> wrote:
> The problem is that you don't understand what dicts are typically used 
> for. Because of the nonliniarity in dict lookups, dicts are used for 
> optimisation.

To me, dicts are first and foremost used when you want a mapping
relationship where the key is not a valid list index (non-negative
integer).  They are also useful when the key space is very sparse.
Imagine I had the following relationship:

 1               => "one"
 1000            => "one thousand"
 1000000         => "one million"
 1000000000      => "one billion"
 1000000000000   => "one trillion"

I could deal with a map that had linear lookup time, but couldn't
afford the linear memory requirements.  Yes, it's important that dicts
are fast, but that's not the primary thing that makes me pick a dict
vs. some other container.  In some situations, it may be, but not
always.  I'm a "Get it working first, optimize it later" kind of guy.

BTW, when you say nonlinear, I think you really mean sublinear.
Quadratic is nonlinear too, but I don't think very many people would
be happy with quadratic dictionary lookup times :-)

More information about the Python-list mailing list