list of dictionaries

Emile van Sebille emile at fenx.com
Wed Jun 27 23:33:11 CEST 2001


Note changes below

--

Emile van Sebille
emile at fenx.com

---------
"George Thomas" <george at cc.gatech.edu> wrote in message
news:3B3A4B97.5F4EB1F1 at cc.gatech.edu...
> Hi everyone,
> As part of a project, I've ended up deciding to use a list of
> dictionaries. However, I don't seem to be getting the "expected" results
> of the append() operation on the list object. Here's some sample code to
> illustrate the problem.
>
> import string
>
> dict_entry = {
>         'a':'',
>         'b':'',
>         'c':'',
>         'd':''
> }

#This creates a single dict that gets appended each time
#by creating a new dict for each entry as per below
#you don't really need this anymore.


>
> a1 = "apples"
> a2 = "oranges"
> b1 = "bananas"
> b2 = "peaches"
> c1 = "coconuts"
> c2 = "pears"
> d1 = "dalmatians"
> d2 = "pecans"
>

dict_entry = {}
# create a new dict

> sample_list = []
> dict_entry['a'] = a1
> dict_entry['b'] = b1
> dict_entry['c'] = c1
> dict_entry['d'] = d1
> sample_list.append(dict_entry)
>

dict_entry = {}
# create a new dict

> dict_entry['a'] = a2
> dict_entry['b'] = b2
> dict_entry['c'] = c2
> dict_entry['d'] = d2
> sample_list.append(dict_entry)
>
> It's something subtle or I'm making a stupid mistake somewhere. Either
> ways, can someone see what's going on ?
> ------------------------------------------------------------
> George Thomas
> ------------------------------------------------------------





More information about the Python-list mailing list