How to update value in dictionary?

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

    How to update value in dictionary?

    dict.update({"a ":1}) SETS the dict item "a" 's value to 1.

    i want to increase it by 1. isnt that possible in an easy way? I
    should use a tuple for this?
  • Iain King

    #2
    Re: How to update value in dictionary?

    On Aug 27, 2:40 pm, ssecorp <circularf...@g mail.comwrote:
    dict.update({"a ":1}) SETS the dict item "a" 's value to 1.
    >
    i want to increase it by 1. isnt that possible in an easy way? I
    should use a tuple for this?
    dict["a"] += 1

    Iain

    Comment

    • Diez B. Roggisch

      #3
      Re: How to update value in dictionary?

      ssecorp wrote:
      dict.update({"a ":1}) SETS the dict item "a" 's value to 1.
      >
      i want to increase it by 1. isnt that possible in an easy way? I
      should use a tuple for this?

      1) Don't use dict as name for a dictionary, it shadows the type dict

      2) setdefault is your friend

      d = {}
      d['a'] = d.setdefault('a ', 1) + 1

      Diez


      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: How to update value in dictionary?

        ssecorp wrote:
        dict.update({"a ":1}) SETS the dict item "a" 's value to 1.
        That works but it's not the right way to do that, use this:

        d["a"] = 1

        Bye,
        bearophile

        Comment

        • mblume

          #5
          Re: How to update value in dictionary?

          Am Wed, 27 Aug 2008 15:45:13 +0200 schrieb Diez B. Roggisch:
          >
          >dict.update({" a":1}) SETS the dict item "a" 's value to 1.
          >>
          >i want to increase it by 1. isnt that possible in an easy way?
          >I should use a tuple for this?
          >
          >
          1) Don't use dict as name for a dictionary, it shadows the type dict
          >
          2) setdefault is your friend
          >
          d = {}
          d['a'] = d.setdefault('a ', 1) + 1
          >
          d['a'] = d.get('a', 1) + 1

          seems to me a little better, as d['a'] doesn't get set twice, right?

          Curious
          Martin

          Comment

          • Fredrik Lundh

            #6
            Re: How to update value in dictionary?

            mblume wrote:
            >2) setdefault is your friend
            >>
            >d = {}
            >d['a'] = d.setdefault('a ', 1) + 1
            >>
            d['a'] = d.get('a', 1) + 1
            >
            seems to me a little better, as d['a'] doesn't get set twice, right?
            setdefault is pronounced "get, and set if necessary". it only updates
            the dictionary if the key isn't already there:
            >>help({}.setde fault)
            Help on built-in function setdefault:

            setdefault(...)
            D.setdefault(k[,d]) -D.get(k,d), also set D[k]=d if k not in D

            (since setdefault is a method, the second argument is evaluated whether
            it's used or not, but that's true for your approach as well, and isn't
            much of a problem for a light-weight object like the integer 1.)

            </F>

            Comment

            • MRAB

              #7
              Re: How to update value in dictionary?

              On Aug 27, 8:01 pm, Fredrik Lundh <fred...@python ware.comwrote:
              mblume wrote:
              2) setdefault is your friend
              >
              d = {}
              d['a'] = d.setdefault('a ', 1) + 1
              >
              d['a'] = d.get('a', 1) + 1
              >
              seems to me a little better, as d['a'] doesn't get set twice, right?
              >
              setdefault is pronounced "get, and set if necessary".  it only updates
              the dictionary if the key isn't already there:
              >
               >>help({}.setde fault)
              Help on built-in function setdefault:
              >
              setdefault(...)
                   D.setdefault(k[,d]) -D.get(k,d), also set D[k]=d if k not in D
              >
              (since setdefault is a method, the second argument is evaluated whether
              it's used or not, but that's true for your approach as well, and isn't
              much of a problem for a light-weight object like the integer 1.)
              >
              Both

              d['a'] = d.setdefault('a ', 1) + 1

              and

              d['a'] = d.get('a', 1) + 1

              will set d['a'] to 2 if 'a' isn't initially in d.

              Comment

              Working...