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 -- http://mail.python.org/mailman/listinfo/python-list