What is a perl hash in python

sturlamolden sturlamolden at yahoo.no
Fri Jan 12 21:18:32 EST 2007


Karyn Williams wrote:
> I am new to Pyton. I am trying to modify and understand a script someone
> else wrote. I am trying to make sense of the following code snippet. I know
> line 7 would be best coded with regex. I first would like to understand
> what was coded originally. thelistOut looks like a hash to me (I'm more
> familiar with perl).

thelistOut seems to be a list of tuples. It also seems that one of the
tuple elements are a list containing a single string. To be honest,
this is one of the most ugly examples of Python code I have ever seen.
I am not sure I would trust code written like this at all. One can very
often tell the competence of the programmer from the looks of the code.

To answer the subject: An associative container in Python is called a
'dictionary'. CPython dictonaries are implemented using hash tables
(and one of the fastest hashing algorithms known to man). There is
nothing in the Python semantics that mandates this particular
implementation of dictionaries, though. Balanced binary trees could
have been used instead of hashes, as they usually are in STL's
associative containers, but in CPython a dictionary is implemented with
a hash table under the hood.

Dictionaries work like this:

mydict = { key1 : val1, key2 : val2, key3 : val3 }
oldval3 = mydict[key3] 
mydict[key3] = newval3
mydict[key4] = val4




More information about the Python-list mailing list