How do I get a reference to a KEY value of a dictionary?

Aahz aahz at pythoncraft.com
Sat Aug 2 07:10:16 CEST 2003


In article <645db655.0307311636.71923378 at posting.google.com>,
Andy C <andychup at yahoo.com> wrote:
>
>Basically I want to create a graph with an adjacency list
>representation, but I don't want any of the adjacency lists to have
>duplicate strings when it is avoidable.  I have a function createEdge
>that adds an edge to the graph.  The arguments will be distinct since
>they are read from text files.  But basically I want to use the
>dictionary as a string pool, and if the argument string equals
>something in the pool already, don't use the argument string, just a
>use a reference to something in the string pool already.

That makes some sense, but why use the string object for both key and
value?  Just do

    d[key] = True

You could optimize your coding style (if not your speed) in Python 2.3
by using sets.  I'd recommend against using intern(), because in Python
2.2 and earlier, interned strings *never* get garbage collected.  Even
in Python 2.3, you may end up with worse memory behavior.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

This is Python.  We don't care much about theory, except where it intersects 
with useful practice.  --Aahz




More information about the Python-list mailing list