When to clear a dictionary...

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

    When to clear a dictionary...

    What is the benefit of clearing a dictionary, when you can just reassign
    it as empty? Similarly, suppose I generate a new dictionary b, and need
    to have it accessible from a. What is the best method, under which
    circumstances?
    >>import some_function
    >>a = {1:2,3:4}
    >>b = {1:2:4:3}
    >>a.clear()
    >>a.update(b)
    >>a = {1:2,3:4}
    >>b = {1:2,4:3}
    >>for key in b:
    .... a[key] = b[key]
    >>a = {1:2,3:4}
    >>b = {1:2,4:3}
    >>a = b
  • Bill Jackson

    #2
    Re: When to clear a dictionary...

    Bill Jackson wrote the following on 04/20/2007 09:48 AM:
    >>import some_function
    >
    >>a = {1:2,3:4}
    >>b = {1:2:4:3}
    >>a.clear()
    >>a.update(b)
    >
    >>a = {1:2,3:4}
    >>b = {1:2,4:3}
    >>for key in b:
    .... a[key] = b[key]
    Clearly, this won't have the same result as the other two examples.
    >
    >>a = {1:2,3:4}
    >>b = {1:2,4:3}
    >>a = b
    >

    Comment

    • Larry Bates

      #3
      Re: When to clear a dictionary...

      Bill Jackson wrote:
      What is the benefit of clearing a dictionary, when you can just reassign
      it as empty?
      If you have objects that point to the dictionary (something like a cache)
      then you want to clear the existing dictionary instead of just assigning
      it to empty. If nothing points to it, assigning it to empty is fast and
      you can let garbage collection do the rest.
      Similarly, suppose I generate a new dictionary b, and need
      to have it accessible from a. What is the best method, under which
      circumstances?
      >
      >>>import some_function
      >
      >>>a = {1:2,3:4}
      >>>b = {1:2:4:3}
      >>>a.clear()
      >>>a.update(b )
      >
      >>>a = {1:2,3:4}
      >>>b = {1:2,4:3}
      >>>for key in b:
      ... a[key] = b[key]
      >
      >>>a = {1:2,3:4}
      >>>b = {1:2,4:3}
      >>>a = b
      >
      Syntax error in the first example but if you fix that the first two are
      equivalent (but I would suspect that the second would be faster for large
      dictionaries).

      The third example both a and b point to the same dictionary after the a=b
      which you can see from:
      >>a is b
      True
      >>id(a)
      31760224
      >>id(b)
      31760224
      >>>
      -Larry

      Comment

      • Gabriel Genellina

        #4
        Re: When to clear a dictionary...

        En Fri, 20 Apr 2007 14:28:00 -0300, Larry Bates <larry.bates@we bsafe.com>
        escribió:
        Bill Jackson wrote:
        >What is the benefit of clearing a dictionary, when you can just reassign
        >it as empty?
        >
        If you have objects that point to the dictionary (something like a cache)
        then you want to clear the existing dictionary instead of just assigning
        it to empty. If nothing points to it, assigning it to empty is fast and
        you can let garbage collection do the rest.
        For an actual comparision, see Alex Martelli posts a few days ago:

        >>>>a = {1:2,3:4}
        >>>>b = {1:2:4:3}
        >>>>a.clear()
        >>>>a.update( b)
        >>
        >>>>a = {1:2,3:4}
        >>>>b = {1:2,4:3}
        >>>>for key in b:
        >... a[key] = b[key]
        >>
        Syntax error in the first example but if you fix that the first two are
        equivalent (but I would suspect that the second would be faster for large
        dictionaries).
        It's the other way; the first method contains a single Python function
        call and most of the work is done in C code; the second does the iteration
        in Python code and is about 4x slower.
        python -m timeit -s "b=dict.fromkey s(range(10000)) ;a={}" "a.update(b )"
        100 loops, best of 3: 10.2 msec per loop
        python -m timeit -s "b=dict.fromkey s(range(10000)) ;a={}" "for key in b:
        a[key]=b[key]"
        10 loops, best of 3: 39.6 msec per loop

        --
        Gabriel Genellina

        Comment

        • Larry Bates

          #5
          Re: When to clear a dictionary...

          Gabriel Genellina wrote:
          En Fri, 20 Apr 2007 14:28:00 -0300, Larry Bates
          <larry.bates@we bsafe.comescrib ió:
          >
          >Bill Jackson wrote:
          >>What is the benefit of clearing a dictionary, when you can just reassign
          >>it as empty?
          >>
          >If you have objects that point to the dictionary (something like a cache)
          >then you want to clear the existing dictionary instead of just assigning
          >it to empty. If nothing points to it, assigning it to empty is fast and
          >you can let garbage collection do the rest.
          >
          For an actual comparision, see Alex Martelli posts a few days ago:

          >
          >>>>>a = {1:2,3:4}
          >>>>>b = {1:2:4:3}
          >>>>>a.clear( )
          >>>>>a.update(b )
          >>>
          >>>>>a = {1:2,3:4}
          >>>>>b = {1:2,4:3}
          >>>>>for key in b:
          >>... a[key] = b[key]
          >>>
          >
          >Syntax error in the first example but if you fix that the first two are
          >equivalent (but I would suspect that the second would be faster for large
          >dictionaries ).
          >
          It's the other way; the first method contains a single Python function
          call and most of the work is done in C code; the second does the
          iteration in Python code and is about 4x slower.
          >
          >python -m timeit -s "b=dict.fromkey s(range(10000)) ;a={}" "a.update(b )"
          100 loops, best of 3: 10.2 msec per loop
          >
          >python -m timeit -s "b=dict.fromkey s(range(10000)) ;a={}" "for key in
          >b: a[key]=b[key]"
          10 loops, best of 3: 39.6 msec per loop
          >
          --Gabriel Genellina
          That is what I meant to say, thanks for catching the error.

          -Larry

          Comment

          • Steven D'Aprano

            #6
            Re: When to clear a dictionary...

            On Fri, 20 Apr 2007 09:48:07 -0700, Bill Jackson wrote:
            What is the benefit of clearing a dictionary, when you can just reassign
            it as empty?
            They are two different things. In the first place, you clear the
            dictionary. In the second place, you reassign the name to a new object
            (which may be an empty dictionary, or anything else) while leaving the
            dictionary as-is.

            Here's an example of clearing the dictionary.
            >>adict = {1:"parrot"}
            >>bdict = adict
            >>adict.clear ()
            >>bdict
            {}

            Because both adict and bdict point to the same dictionary object,
            clearing it results in an empty dictionary no matter what name (if any!)
            you use to refer to it.

            Here's an example of re-assigning the name.
            >>adict = {1:"parrot"}
            >>bdict = adict
            >>adict = {} # re-assign the name to an empty dict
            >>bdict
            {1: 'parrot'}

            Although adict and bdict both start off pointing to the same dictionary,
            once you re-assign the name adict, they now point to different
            dictionaries, only one of which is empty.

            In this specific case, if bdict didn't exist, the original dictionary
            would then be garbage-collected and the memory re-claimed. In the C
            implementation of Python (CPython), that will happen immediately; in
            the Java and (I think) .Net implementations of Python (Jython and
            IronPython) it will happen "eventually ", with no guarantee of how long it
            will take.
            Similarly, suppose I generate a new dictionary b, and need
            to have it accessible from a. What is the best method, under which
            circumstances?
            That depends on what you are trying to do.
            >>adict = {1: "parrot"}
            >>bdict = {2: "spam")
            What is it that you want to do?

            (1) "I want the name 'adict' to point to the same dict as bdict."

            Solution:

            adict = bdict


            (2) "I want the data in bdict to update the data in adict, keeping items
            in adict that are not in bdict but replacing them if they are in bdict."

            Solution:

            adict.update(bd ict)


            (3) "I want the data in bdict to be copied into adict, throwing away
            whatever was already there."

            Solution:

            adict.clear()
            adict.update(bd ict)


            (4) "I want the data in bdict to be copied into adict, but keeping what
            was already there."

            Solution:

            for key in bdict:
            if adict.has_key(k ey):
            pass # ignore it
            else:
            adict[key] = bdict[key] # add it


            --
            Steven.

            Comment

            Working...