Appending to dictionary of lists

Alex van der Spek zdoor at xs4all.nl
Tue May 3 16:20:00 EDT 2011


Thank you! Would never have found that by myself.


"Paul Rubin" <no.email at nospam.invalid> wrote in message 
news:7x7ha75zib.fsf at ruckus.brouhaha.com...
> "Alex van der Spek" <zdoor at xs4all.nl> writes:
>> refd=dict.fromkeys(csvr.fieldnames,[])  ...
>> I do not understand why this appends v to every key k each time.
>
> You have initialized every element of refd to the same list.  Try
>
>    refd = dict((k,[]) for k in csvr.fieldnames)
>
> instead. 




More information about the Python-list mailing list