list of dictionaries

David Bolen db3l at fitlinxx.com
Wed Jun 27 22:52:55 EDT 2001


George Thomas <george at cc.gatech.edu> writes:

> Hi,
> 
> That makes sense. However, to make sure I understand completely, I'll
> list the steps in question.
> Given: A single dict object, call it dict (how original!)
> 1. Start with empty list
> 2. Update fields of dict
> 3. append dict to list.
> 4. Update fields of dict.
> 5. append dict to list.
> The thing I can't understand is: even though dict reflects the change in
> (4), the append() operation seems to ignore it completely, giving me a
> copy of the previous contents. Why does this happen ?
> Thanks for the insight on the alternative solution. I guess I'll rest
> easy when the issue above has an explanation :)

That shouldn't be what you are getting, so you may have to be more
explicit (e.g., your original post didn't actually show the contents
of the objects after the execution).

If I execute the code you had in your original posting:

    dict_entry = {
	    'a':'',
	    'b':'',
	    'c':'',
	    'd':''
    }

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

    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['a'] = a2
    dict_entry['b'] = b2
    dict_entry['c'] = c2
    dict_entry['d'] = d2
    sample_list.append(dict_entry)

then here's what I have in the end in dict_entry and sample_list
(either under Python 1.5.2 or 2.0):

    >>> dict_entry
    {'d': 'pecans', 'b': 'peaches', 'c': 'pears', 'a': 'oranges'}

    >>> pprint.pprint(sample_list)
    [{'d': 'pecans', 'b': 'peaches', 'c': 'pears', 'a': 'oranges'},
     {'d': 'pecans', 'b': 'peaches', 'c': 'pears', 'a': 'oranges'}]

Both entries in the list (that were appended separately) are
references to the same single dictionary (dict_entry), and must
reflect the contents of that single dictionary object - which thus
will be the second set of values that you stored under the keys 'a'
through 'd'.  So that's what I'd expect to find, given that you have
really just appended to the list two references to the same object.

But you seem to be saying that's not the case when you run this, but
instead you see the second set of values when you examine dict_entry,
but see the first set of values in the dictionary objects within
sample_list.  If that's really what you are saying, then something is
definitely not working as it should, or you're not running exactly the
code shown above - perhaps you simplified the example thinking you
were removing something innocuous?

If you could give an actual example of what values you find in the
objects involved, it might help explain further.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list