removing dictionary key-pair

Discussion in 'Python' started by JD, Jun 9, 2006.

  1. JD

    JD Guest

    Hello,

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

    Thanks
    JD, Jun 9, 2006
    #1
    1. Advertising

  2. JD

    Guest

    JD>I try to remove a dictionary key-pair (remove an entry),

    >>> d = {1:2, 3:4}
    >>> d

    {1: 2, 3: 4}
    >>> del d[1]
    >>> d

    {3: 4}

    Bye,
    bearophile
    , Jun 9, 2006
    #2
    1. Advertising

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


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

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Jun 9, 2006
    #3
  4. JD

    Guest

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


    Assuming you know the key:

    d = {"foo":1,"bar":2}
    print d
    del(d["foo"])
    print d
    , Jun 9, 2006
    #4
  5. bruno at modulix wrote:
    > JD wrote:
    >
    >>Hello,
    >>
    >>I try to remove a dictionary key-pair (remove an entry),
    >>but I'm unsuccessful. Does anyone know how to achieve this?
    >>
    >>Thanks

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


    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 ''.split('@')])"
    bruno at modulix, Jun 9, 2006
    #5
  6. JD

    Guest

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


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

    print d

    del d['a']

    print d
    , Jun 9, 2006
    #6
  7. JD

    Steve Holden Guest

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


    >>> d = {1: "one", 2: "two", 3: "three"}
    >>> del d[2]
    >>> d

    {1: 'one', 3: 'three'}
    >>>


    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
    Steve Holden, Jun 9, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    1,477
    Roedy Green
    Oct 30, 2005
  2. Replies:
    1
    Views:
    534
    Daniel Pitts
    Nov 16, 2007
  3. M P
    Replies:
    1
    Views:
    435
  4. Samuel
    Replies:
    8
    Views:
    110
    [MSFT]
    Oct 14, 2004
  5. Token Type
    Replies:
    13
    Views:
    406
    Token Type
    Oct 9, 2012
Loading...

Share This Page