Better dict of dicts

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Apr 19 19:19:17 EDT 2007


On Thu, 19 Apr 2007 17:40:27 -0400, John Bauman wrote:

> Adam Atlas wrote:
>> On Apr 19, 5:24 pm, Bill Jackson <jack... at hotmail.com> wrote:
>>> I have a dictionary of dictionaries where the keys are typically very
>>> long tuples and repeated in each inner dictionary.  The dictionary
>>> representation is nice because it handles sparseness well...and it is
>>> nice to be able to look up values based on a string rather than a
>>> number.  However, since my keys are quite long, I worry that I am
>>> wasting a lot of memory.
>> 
>> I wouldn't worry about it. Try doing hash('string_2') in the
>> interpreter -- the output thereof is what's really being used as the
>> key. It doesn't use up any more memory than the integer 2.
>> 
> Are you sure about that? Most dictionaries need to store the actual key, 
> in case of a collision, so when you lookup a key they can tell which 
> you're really looking for.


The key is already stored, so long as the string 'string_2' exists. And it
will continue to exist so long as the dictionary includes it as a key. An
extra copy isn't made (unless you make an extra copy yourself).

In other words, the dictionary stores a reference to the string, not a
copy of it:

>>> s = "this is a long string"
>>> d = {s: 1}
>>> id(s)
-1209073840
>>> id(d.keys()[0])
-1209073840


-- 
Steven.




More information about the Python-list mailing list