assign dict by value to list
Steve Holden
steve at holdenweb.com
Tue Sep 6 23:40:20 EDT 2005
Phill Atwood wrote:
> Newbie question:
>
> I have a dictionary called "rec". And a list called "db". db is my
> database. rec are to be records in the database. In a loop I'm trying to
> create a record and add it to my database. The key stmt is
>
> db.append( rec )
>
> This works initially but it seems that instead of copying the values in
> rec and adding it to db, only a reference of rec is added to db. Thus
> when I reassign rec with new data and then do another append I end up
> with two records in my database but now the both contain the same data,
> namely the latest version or rec.
>
> So how do I add a dictionary into a list by value rather than by reference?
>
> The complete code is here:
>
[code snipped]
I see you do a rec.clear() to clear out the contents of one record
before creating a new one. Python only *ever* uses references, so why
not just use
rec = {}
instead of rec.clear()?
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list