Complex datastructures in Python

Greg Gallagher ggallag at red-bean.com
Tue Jun 27 17:33:47 EDT 2000


I'm a little disheartened here... I know how to do this in Perl, but I'm lost in Python and 
searching for 'complex data structures' is landing me nowhere.  Basically,  I want
a dictionary whosevalues are lists of dictionaries.  Simple.
I want to basically loop through some data and so something like this:

while loop:
   new_forward = {'ip' : ip_address,
                  'hostname' : hostname,
                  'mac_address' : mac_address}

   forward[domain].append(new_forward)


My code is simplified from what I want to do, but basically it loops through some data
in a file, picks out come fields (ip, hostname, etc.) and puts them in a dictionary,
new_forward.  I want a list of dictionaries key'd by the value 'domain', also
picked out of the fields in the file.  In perl ingo, this would be a hash of
hashes, basically.

Simply put, what kind of operations can or should I be doing, because it never likes
the append.  I've even tried something like this:

   forward[domain] = (forward[domain], new_forward)

And that almost made the structure I want but, alas, it was an empty dictionary.  

Any pointers?  Also, any good documentation for 'complex datastructures', ala Perl's
Cookbook ?

thanks!!

Greg Gallagher
greg at red-bean.com
http://www.red-bean.com/




More information about the Python-list mailing list