Dictionary problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elena Schulz

    Dictionary problem

    Hi,

    I've the following code:

    myList = []
    for i in range(3) :
    myDict['id']=i
    myList.append(m yDict)

    myList becomes: [{'id':2}, {'id':2}, {'id':2}] but I want: [{'id':0},
    {'id':1}, {'id':2}]

    thanx for any hint how to achieve that

    -- Greetings, Elena

    (Please answer to my mail address directly as I am currently not subscribed
    to this list, thanks)


  • Alex Martelli

    #2
    Re: Dictionary problem

    <posted & mailed>

    Elena Schulz wrote:
    [color=blue]
    > myList = []
    > for i in range(3) :
    > myDict['id']=i
    > myList.append(m yDict)
    >
    > myList becomes: [{'id':2}, {'id':2}, {'id':2}] but I want: [{'id':0},
    > {'id':1}, {'id':2}][/color]

    So, you don't want to append myDict itself, but, rather, a copy of it.
    [color=blue]
    > thanx for any hint how to achieve that[/color]

    Instead of:

    myList.append(m yDict)

    which appends myDict itself, append a copy, e.g.:

    myList.append(m yDict.copy())

    or

    myList.append(d ict(myDict))

    Python doesn't make copies by default: when you want a copy, you ask
    for one explicitly, as in these two examples.
    [color=blue]
    > (Please answer to my mail address directly as I am currently not
    > subscribed to this list, thanks)[/color]

    Answering by both posting and mailing as requested.


    Alex

    Comment

    • Duncan Booth

      #3
      Re: Dictionary problem

      "Elena Schulz" <elena.schulz@g mx.net> wrote in
      news:mailman.79 9.1069086944.70 2.python-list@python.org :
      [color=blue]
      > I've the following code:
      >
      > myList = []
      > for i in range(3) :
      > myDict['id']=i
      > myList.append(m yDict)
      >
      > myList becomes: [{'id':2}, {'id':2}, {'id':2}] but I want: [{'id':0},
      > {'id':1}, {'id':2}]
      >
      > thanx for any hint how to achieve that[/color]

      Well, firstly I would say that you don't have the code you posted, because
      if you did you would be getting:

      NameError: name 'myDict' is not defined

      Anyway, if you want your list to contain three different dictionaries
      instead of three references to the same dictionary, then you have to create
      a new dictionary each time. This should do for your example:

      myList = []
      for i in range(3):
      myList.append({ 'id':i})

      Remember that Python never, ever, makes a copy of an object unless you
      explicitly tell it to make a copy. Usually all it does is shuffle around
      references to the same objects.

      If you had a load of other values in your dictionary, and wanted to have
      different dictionaries with just the id field different, you could do
      something like:

      myDict = { 'a': 1, 'b': 2 }
      myList = []
      for i in range(3):
      myDict['id'] = i
      myList.append(m yDict.copy())


      --
      Duncan Booth duncan@rcp.co.u k
      int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
      "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

      Comment

      • Peter Abel

        #4
        Re: Dictionary problem

        "Elena Schulz" <elena.schulz@g mx.net> wrote in message news:<mailman.7 99.1069086944.7 02.python-list@python.org >...[color=blue]
        > Hi,
        >
        > I've the following code:
        >
        > myList = []
        > for i in range(3) :
        > myDict['id']=i
        > myList.append(m yDict)
        >
        > myList becomes: [{'id':2}, {'id':2}, {'id':2}] but I want: [{'id':0},
        > {'id':1}, {'id':2}]
        >
        > thanx for any hint how to achieve that
        >
        > -- Greetings, Elena
        >
        > (Please answer to my mail address directly as I am currently not subscribed
        > to this list, thanks)[/color]

        If your desired result is a list whose elements are bound to different
        dictionaries, it won't be necessary to bind a dictionary to a variablename
        and the bind a copy of it to an new element of a list.
        So the shoretest way for me is to append an explicit new dictionary
        at the end of list, assumed that your dictionary is as easy as shown
        in your example.
        So the following works for me so far:
        [color=blue][color=green][color=darkred]
        >>> myList=[]
        >>> for i in range(3):[/color][/color][/color]
        .... myList.append({ 'id':i})
        ....[color=blue][color=green][color=darkred]
        >>> myList[/color][/color][/color]
        [{'id': 0}, {'id': 1}, {'id': 2}][color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Regards
        Peter

        Comment

        Working...