Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4571

Re: Appending to dictionary of lists

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: Appending to dictionary of lists
References <4dc05c8b$0$41117$e4fe514c@news.xs4all.nl>
Date 2011-05-03 12:56 -0700
Message-ID <7x7ha75zib.fsf@ruckus.brouhaha.com> (permalink)
Organization Nightsong/Fort GNOX

Show all headers | View raw


"Alex van der Spek" <zdoor@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Appending to dictionary of lists "Alex van der Spek" <zdoor@xs4all.nl> - 2011-05-03 21:50 +0200
  Re: Appending to dictionary of lists Paul Rubin <no.email@nospam.invalid> - 2011-05-03 12:56 -0700
    Re: Appending to dictionary of lists "Alex van der Spek" <zdoor@xs4all.nl> - 2011-05-03 22:20 +0200

csiph-web