appending to dict

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

    appending to dict

    Hi
    is there anyway appending to dictionary?
    list has this feature
    [color=blue][color=green][color=darkred]
    >>>a = []
    >>>a.append(1 )
    >>>print a[/color][/color][/color]
    [1]

    but dictionary can't
    i wanna do like this thing[color=blue][color=green][color=darkred]
    >>>a = {1, 2}
    >>>a.append(3 , 4) -> This is just my idea :@
    >>>print a[/color][/color][/color]
    {1:2, 3:4}

    so i try this feature like[color=blue][color=green][color=darkred]
    >>>a = {1, 2}
    >>>a = dict(zip(a.keys ().append(3), a.values().appe nd(4)))[/color][/color][/color]
    but this make's error
    because a.keys().append (3) makes 'None' (i can't understand this :( )

    so i try[color=blue][color=green][color=darkred]
    >>>import copy
    >>>a = dict(zip(copy.c opy(a.keys()).a ppend(3),[/color][/color][/color]
    copy.copy(a.val ues()).append(4 )))
    and
    a = dict(zip(copy.d eepcopy(a.keys( )).append(3),
    copy.deepcopy(a .values()).appe nd(4)))
    but this make's error, too

    why a.keys().append (3) makes 'None' ?
    and is there anyway appending to dictionary?

    any post will be appreciated :)
  • Holger Türk

    #2
    Re: appending to dict



    bucket79 wrote:[color=blue]
    > why a.keys().append (3) makes 'None' ?[/color]

    Because .append modifies the list in place. It does
    not return a new list.
    [color=blue]
    > and is there anyway appending to dictionary?[/color]

    key/value pairs in a dictionary are not ordered. There
    is no "last pair". So you can't append to a dictionary.
    But you can update it with another dictionary.
    Use something like: adict.update (anotherdict)

    keys () returns a list of keys in the dictionary.
    The order of the elements in this list is arbitrary
    (or somehow ordered afterwards). But there is no
    real list of keys in the dictionary itself.

    Greetings,

    Holger

    Comment

    • Hugh Macdonald

      #3
      Re: appending to dict

      On 14 May 2004 02:40:00 -0700
      bucket79@daum.n et (bucket79) wrote:
      [color=blue]
      > Hi
      > is there anyway appending to dictionary?
      > list has this feature
      >[color=green][color=darkred]
      > >>>a = []
      > >>>a.append(1 )
      > >>>print a[/color][/color]
      > [1]
      >
      > but dictionary can't
      > i wanna do like this thing[color=green][color=darkred]
      > >>>a = {1, 2}
      > >>>a.append(3 , 4) -> This is just my idea :@
      > >>>print a[/color][/color]
      > {1:2, 3:4}[/color]

      a = {1:2}
      a.update({3:4})



      --
      Hugh Macdonald
      The Moving Picture Company

      Comment

      • Peter Hansen

        #4
        Re: appending to dict

        bucket79 wrote:
        [color=blue]
        > is there anyway appending to dictionary?
        > list has this feature
        >[color=green][color=darkred]
        >>>>a = []
        >>>>a.append( 1)
        >>>>print a[/color][/color]
        > [1]
        >
        > but dictionary can't
        > i wanna do like this thing
        >[color=green][color=darkred]
        >>>>a = {1, 2}
        >>>>a.append( 3, 4) -> This is just my idea :@
        >>>>print a[/color][/color]
        > {1:2, 3:4}[/color]

        Did you know that the following actually works?
        [color=blue][color=green][color=darkred]
        >>> a = {1: 2} # note: your syntax was wrong here
        >>> print a[/color][/color][/color]
        {1: 2}[color=blue][color=green][color=darkred]
        >>> a[3] = 4
        >>> print a[/color][/color][/color]
        {1: 2, 3: 4}

        Since, as Holger pointed out, dictionaries aren't ordered,
        the concept implicit in "append" doesn't apply. The more
        appropriate concept "update", however, can be spelled either
        dict.update(), or like what I just showed, for dictionaries.

        -Peter

        Comment

        • Heather Coppersmith

          #5
          Re: appending to dict

          On 14 May 2004 02:40:00 -0700,
          bucket79@daum.n et (bucket79) wrote:
          [color=blue]
          > is there anyway appending to dictionary?
          > list has this feature[/color]
          [color=blue][color=green][color=darkred]
          >>>> a = []
          >>>> a.append(1)
          >>>> print a[/color][/color]
          > [1][/color]
          [color=blue]
          > but dictionary can't
          > i wanna do like this thing[color=green][color=darkred]
          >>>> a = {1, 2}
          >>>> a.append(3, 4) -> This is just my idea :@
          >>>> print a[/color][/color]
          > {1:2, 3:4}[/color]

          Maybe I'm missing something. Try this:

          a = { 1:2 }
          a[ 3 ] = 4

          and then check out



          and



          HTH,
          Heather

          --
          Heather Coppersmith
          That's not right; that's not even wrong. -- Wolfgang Pauli

          Comment

          Working...