removing dictionary key-pair

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

    #1

    removing dictionary key-pair


    Hello,

    I try to remove a dictionary key-pair (remove an entry),
    but I'm unsuccessful. Does anyone know how to achieve this?

    Thanks
  • bearophileHUGS@lycos.com

    #2
    Re: removing dictionary key-pair

    JD>I try to remove a dictionary key-pair (remove an entry),
    [color=blue][color=green][color=darkred]
    >>> d = {1:2, 3:4}
    >>> d[/color][/color][/color]
    {1: 2, 3: 4}[color=blue][color=green][color=darkred]
    >>> del d[1]
    >>> d[/color][/color][/color]
    {3: 4}

    Bye,
    bearophile

    Comment

    • bruno at modulix

      #3
      Re: removing dictionary key-pair

      JD wrote:[color=blue]
      > Hello,
      >
      > I try to remove a dictionary key-pair (remove an entry),
      > but I'm unsuccessful. Does anyone know how to achieve this?
      >
      > Thanks[/color]

      mydict = {"key" : "value"}
      del mydict(key)

      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • akameswaran@gmail.com

        #4
        Re: removing dictionary key-pair


        JD wrote:[color=blue]
        > Hello,
        >
        > I try to remove a dictionary key-pair (remove an entry),
        > but I'm unsuccessful. Does anyone know how to achieve this?
        >
        > Thanks[/color]

        Assuming you know the key:

        d = {"foo":1,"bar": 2}
        print d
        del(d["foo"])
        print d

        Comment

        • bruno at modulix

          #5
          Re: removing dictionary key-pair

          bruno at modulix wrote:[color=blue]
          > JD wrote:
          >[color=green]
          >>Hello,
          >>
          >>I try to remove a dictionary key-pair (remove an entry),
          >>but I'm unsuccessful. Does anyone know how to achieve this?
          >>
          >>Thanks[/color]
          >
          >
          > mydict = {"key" : "value"}
          > del mydict(key)[/color]

          grmf... Typo. This is:

          del mydict['key']

          of course...
          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • danmcleran@yahoo.com

            #6
            Re: removing dictionary key-pair


            JD wrote:[color=blue]
            > Hello,
            >
            > I try to remove a dictionary key-pair (remove an entry),
            > but I'm unsuccessful. Does anyone know how to achieve this?
            >
            > Thanks[/color]

            d = dict(a=1, b=2, c=3)

            print d

            del d['a']

            print d

            Comment

            • Steve Holden

              #7
              Re: removing dictionary key-pair

              JD wrote:[color=blue]
              > Hello,
              >
              > I try to remove a dictionary key-pair (remove an entry),
              > but I'm unsuccessful. Does anyone know how to achieve this?
              >
              > Thanks[/color]
              [color=blue][color=green][color=darkred]
              >>> d = {1: "one", 2: "two", 3: "three"}
              >>> del d[2]
              >>> d[/color][/color][/color]
              {1: 'one', 3: 'three'}[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Love me, love my blog http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              Working...