473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Apr 20 '07 #1
5 1934
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
Apr 20 '07 #2
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

Apr 20 '07 #3
En Fri, 20 Apr 2007 14:28:00 -0300, Larry Bates <la*********@websafe.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:
http://mail.python.org/pipermail/pyt...ch/433027.html
>>>>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.fromkeys(range(10000));a={}" "a.update(b)"
100 loops, best of 3: 10.2 msec per loop
python -m timeit -s "b=dict.fromkeys(range(10000));a={}" "for key in b:
a[key]=b[key]"
10 loops, best of 3: 39.6 msec per loop

--
Gabriel Genellina
Apr 20 '07 #4
Gabriel Genellina wrote:
En Fri, 20 Apr 2007 14:28:00 -0300, Larry Bates
<la*********@websafe.comescribió:
>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:
http://mail.python.org/pipermail/pyt...ch/433027.html
>>>>>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.fromkeys(range(10000));a={}" "a.update(b)"
100 loops, best of 3: 10.2 msec per loop
>python -m timeit -s "b=dict.fromkeys(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
Apr 20 '07 #5
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(bdict)
(3) "I want the data in bdict to be copied into adict, throwing away
whatever was already there."

Solution:

adict.clear()
adict.update(bdict)
(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(key):
pass # ignore it
else:
adict[key] = bdict[key] # add it
--
Steven.

Apr 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
0
by: BLinfield | last post by:
I'm new to .Net and I inherited a Web App from some guys who left the company. The App stores some stuff from the Database in a Dictionary so that the first time the data is accessed it is slow but...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.