Complex datastructures in Python

Rainer Deyke root at rainerdeyke.com
Tue Jun 27 19:20:50 EDT 2000


Greg Gallagher <ggallag at red-bean.com> wrote in message
news:8jb6jr$2uov$1 at news.enteract.com...
>
> 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)

This should work, assuming forward[domain] exists and is a list.

try:
  tmp = forward[domain] # Does forward[domain] exist?
except:
  forward[domain] = []
forward[domain].append(new_forward)

There is almost certainly a better way to check if forward[domain] exists,
but I'm too lazy to check.

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

This creates nested tuples - not what you want, I think.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware action/role-playing games      -      http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list