By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

how can I clear a dictionary in python

P: n/a
Hi,

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?

Thank you.

Mar 28 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a

On 29.03.2007, at 00:38, Ma***********@gmail.com wrote:
Hi,

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
Reading the Python docs might help.
But before, I would try a dir(myDict).
Maybe you will find an easter-egg
which has exactly the name you are looking for?

cheers - chris

Mar 28 '07 #2

P: n/a
Ma***********@gmail.com wrote:
Hi,

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?

Thank you.
just point myDict to an empty dictionary again

myDict={}

Larry Bates

Mar 28 '07 #3

P: n/a
Ma***********@gmail.com a écrit :
Hi,

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
>>help(dict)
Help on class dict in module __builtin__:

class dict(object)
(...)
| Methods defined here:
(...)
|
| clear(...)
| D.clear() -None. Remove all items from D.
(...)
>>>
d = dict(a=1, b=2)
d
{'a': 1, 'b': 2}
>>d.clear()
d
{}
>>>
Mar 28 '07 #4

P: n/a
In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>Ma***********@gmail.com wrote:
>>
I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?

just point myDict to an empty dictionary again

myDict={}
Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Need a book? Use your library!
Mar 28 '07 #5

P: n/a

On 29.03.2007, at 00:48, Larry Bates wrote:
Ma***********@gmail.com wrote:
>Hi,

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?

Thank you.

just point myDict to an empty dictionary again

myDict={}
This is wrong and not answering the question.
Creating a new dict does not change the dict.
He wants to clear *this* dict, and maybe he
cannot know how many other objects are
referring to this dict.

cheers -- chris
------------------------------------------------------------------
"""pointless questions or useless answers - what do you like more"""
Mar 28 '07 #6

P: n/a
Aahz wrote:
Go back and read Christian's post, then post a followup explaning
why his solution is better than yours. Your explanation should
use id().
I wonder how you two seem to know exactly what the OP wants ...

Regards,
Björn

--
BOFH excuse #335:

the AA battery in the wallclock sends magnetic interference

Mar 28 '07 #7

P: n/a
In article <57*************@mid.individual.net>,
Bjoern Schliessmann <us**************************@spamgourmet.comwrote :
>Aahz wrote:
>>
Go back and read Christian's post, then post a followup explaning
why his solution is better than yours. Your explanation should
use id().

I wonder how you two seem to know exactly what the OP wants ...
We don't. However, we do know exactly what the OP said... (Perhaps you
didn't read the Subject: line?)
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Need a book? Use your library!
Mar 29 '07 #8

P: n/a
On Thu, 29 Mar 2007 01:51:07 +0200, Bjoern Schliessmann wrote:
Aahz wrote:
>Go back and read Christian's post, then post a followup explaning
why his solution is better than yours. Your explanation should
use id().

I wonder how you two seem to know exactly what the OP wants ...
By reading his post perhaps? He asked how to clear a dictionary, not how
to assign an empty dictionary to a name. He already knows how to do that.

(I know that from reading his post too.)
--
Steven.
Mar 29 '07 #9

P: n/a
Aahz wrote:
In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>Ma***********@gmail.com wrote:
>>I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
just point myDict to an empty dictionary again

myDict={}

Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().
I believe he (as many new to Python do) are mired in old
programming thinking that variables "contain" things.
As I'm sure you kno, variables point to things in Python.
I don't believe that there are lots of other objects
pointing to this dictionary. Perhaps the OP can clarify
for us. If there aren't other objects pointing to
this dictionary it would make NO sense to iterate over a
dictionary and delete all the keys/values so I tried to read
between the lines and answer what I believe the OP thought he
was asking. BTW-I didn't see you posting an answer to what
you thought was the "correct" question, just criticizing me
for taking the time to answer what I perceived the OP was
asking.

-Larry
Mar 29 '07 #10

P: n/a
Larry Bates a écrit :
Aahz wrote:
>In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>>Ma***********@gmail.com wrote:
I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
just point myDict to an empty dictionary again

myDict={}
Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old
programming thinking that variables "contain" things.
Does "he" refer to Christian Tismer ? If so, you may want to use google
and correct your beliefs (hint: does 'stackless Python' ring a bell ?).
As I'm sure you kno, variables point to things in Python.
(conceptually) names are keys associated with an object in a given
namespace. FWIW, be assured that Christians knows this pretty well.
I don't believe that there are lots of other objects
pointing to this dictionary.
You just cannot *know* this. This might be true now, this might be false
now, and this might change anytime. Assuming anything here is the road
to disaster. Don't assume, do the righ thing.
Perhaps the OP can clarify
for us. If there aren't other objects pointing to
this dictionary it would make NO sense to iterate over a
dictionary and delete all the keys/values
You don't iterate over anything. Dicts being the central datastructure
in Python, you can bet your ass they are optimized to death (or near
death). If you have any doubt, then still don't assume : verify (hint:
import timeit). And yet even if clearing a dict happens to be a bit
slower than instanciating a new one, rebinding a name and mutating an
object are still two very different concepts in Python, with very
different consequences. The OP explicitely asked for clearing a dict,
not for creating a new one.
so I tried to read
between the lines
Why ?
and answer what I believe the OP thought he
was asking.
What the OP asked was quite clear, and not subject to any
interpretation. And the correct answer is obviously not the one you gave.
BTW-I didn't see you posting an answer to what
you thought was the "correct" question, just criticizing me
for taking the time to answer what I perceived the OP was
asking.
If you cannot understand the difference between criticism and a friendly
correction, nor why you should actually thank the one correcting you
when you're wrong, I guess there's no point trying explaining why
correcting wrong answers on newsgroups is actually important.
Mar 29 '07 #11

P: n/a
Bruno Desthuilliers wrote:
Larry Bates a écrit :
>Aahz wrote:
>>In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
Ma***********@gmail.com wrote:
I create a dictionary like this
myDict = {}
>
and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
just point myDict to an empty dictionary again

myDict={}
Go back and read Christian's post, then post a followup explaning why
his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old
programming thinking that variables "contain" things.

Does "he" refer to Christian Tismer ? If so, you may want to use google
and correct your beliefs (hint: does 'stackless Python' ring a bell ?).
>As I'm sure you kno, variables point to things in Python.

(conceptually) names are keys associated with an object in a given
namespace. FWIW, be assured that Christians knows this pretty well.
>I don't believe that there are lots of other objects
pointing to this dictionary.

You just cannot *know* this. This might be true now, this might be false
now, and this might change anytime. Assuming anything here is the road
to disaster. Don't assume, do the righ thing.
> Perhaps the OP can clarify
for us. If there aren't other objects pointing to
this dictionary it would make NO sense to iterate over a
dictionary and delete all the keys/values

You don't iterate over anything. Dicts being the central datastructure
in Python, you can bet your ass they are optimized to death (or near
death). If you have any doubt, then still don't assume : verify (hint:
import timeit). And yet even if clearing a dict happens to be a bit
slower than instanciating a new one, rebinding a name and mutating an
object are still two very different concepts in Python, with very
different consequences. The OP explicitely asked for clearing a dict,
not for creating a new one.
>so I tried to read
between the lines

Why ?
>and answer what I believe the OP thought he
was asking.

What the OP asked was quite clear, and not subject to any
interpretation. And the correct answer is obviously not the one you gave.
> BTW-I didn't see you posting an answer to what
you thought was the "correct" question, just criticizing me
for taking the time to answer what I perceived the OP was
asking.

If you cannot understand the difference between criticism and a friendly
correction, nor why you should actually thank the one correcting you
when you're wrong, I guess there's no point trying explaining why
correcting wrong answers on newsgroups is actually important.
I stand corrected about my answer, but I'll stick to my assumption
(until told otherwise by the OP) that the question that was asked
wasn't precisely what the OP wanted to accomplish. It was just too
simple to mean what you guys assumed (e.g. complex dictionary pointed
to by lots of other objects, etc.). I don't mind being corrected with
the proper information, but Aahz's post doesn't make any sense. He said
"Go back and read Christian's answer...". Christian's answers were not
answers to the OP's question at all. Here is what shows up in my
newsreader:

'''
Reading the Python docs might help.
But before, I would try a dir(myDict).
Maybe you will find an easter-egg
which has exactly the name you are looking for?

cheers - chris
'''

and

'''
This is wrong and not answering the question.
Creating a new dict does not change the dict.
He wants to clear *this* dict, and maybe he
cannot know how many other objects are
referring to this dict.

cheers -- chris
'''

You appear to be the only one that apparently posted the correct
answer myDict.clear() for which I thank you. I learned two things
today: 1) answer EXACTLY the question that is asked and 2) dicts
have a clear method that is safer than my answer.

-Larry
Mar 29 '07 #12

P: n/a
This little squabble got me thinking. I normally just use the
myDict={} method of "clearing" a
dictionary when I know there are no other references to it. However, I
wonder how the
efficiency of relying on the garbage collector to clear a dictionary
compares with using the
"clear" method. Does anyone know?

Mar 30 '07 #13

P: n/a
Russ <uy*******@sneakemail.comwrote:
This little squabble got me thinking. I normally just use the
myDict={} method of "clearing" a
dictionary when I know there are no other references to it. However, I
wonder how the
efficiency of relying on the garbage collector to clear a dictionary
compares with using the
"clear" method. Does anyone know?
Well, anybody who bothers to *MEASURE* can start building an idea.

When the dict's already empty:

brain:~ alex$ python -mtimeit 'd={}'
10000000 loops, best of 3: 0.113 usec per loop
brain:~ alex$ python -mtimeit 'd={}; d={}'
1000000 loops, best of 3: 0.207 usec per loop
brain:~ alex$ python -mtimeit 'd={}; d.clear()'
1000000 loops, best of 3: 0.316 usec per loop

Making one dict costs about 100 nanoseconds, making two of them costs
about 200 (sensible), making one and clearing it 300 (so just the
clearing, on an empty dict, about 200 nanoseconds).

Unfortunately, microbenchmarks of operations which do change the state
their timing depend on are trickier. Still, here's an attempt:

brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy()'
100000 loops, best of 3: 6.73 usec per loop
brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy();d={}'
100000 loops, best of 3: 6.76 usec per loop
brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy()'
100000 loops, best of 3: 6.73 usec per loop
brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy();d={}'
100000 loops, best of 3: 6.78 usec per loop
brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy();d.clear()'
100000 loops, best of 3: 6.94 usec per loop
brain:~ alex$ python -mtimeit -s'D=dict.fromkeys(xrange(99))'
'd=D.copy();d.clear()'
100000 loops, best of 3: 6.93 usec per loop

Here, making a middly-size dict costs about 6730 nanoseconds.
Making an empty one as well adds 30-50 nanoseconds; clearing the middly
one instead ads 200 nanoseconds or so.

It would appear that clearing an existing dict costs about twice as much
(or more) as making a new one (200 nanoseconds vs 100 nanoseconds or
less) for different sizes of the existing dict.

This is on a 2GHz Intel Core Duo with Python 2.5 -- measures of a few
tens of nanoseconds can be "noisy" enough to change substantially on
different release of Python or different CPUs.

Fortunately, it's unusual to care about such tiny performance issues
(here, the time to build the dictionary would normally swap the time to
clear it, OR to assign an empty one instead, by over an order of
magnitude, so...).
Alex
Mar 30 '07 #14

P: n/a
In article <KP******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>Aahz wrote:
>In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>>Ma***********@gmail.com wrote:

I create a dictionary like this
myDict = {}

and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
just point myDict to an empty dictionary again

myDict={}

Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old programming
thinking that variables "contain" things. As I'm sure you kno,
variables point to things in Python. I don't believe that there are
lots of other objects pointing to this dictionary. Perhaps the OP
can clarify for us. If there aren't other objects pointing to this
dictionary it would make NO sense to iterate over a dictionary and
delete all the keys/values so I tried to read between the lines and
answer what I believe the OP thought he was asking.
Then you should explain why you didn't answer the question that was
asked. Answering a different question without explanation makes your
answer irrelevant at best, wrong at worst.
>BTW-I didn't see you posting an answer to what you thought was the
"correct" question, just criticizing me for taking the time to answer
what I perceived the OP was asking.
Because Christian already answered the question! Granted, he chose a
pseudo-Socratic approach, but with the OP already using the word "clear"
in the Subject: line, I think that was entirely reasonable.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Need a book? Use your library!
Apr 3 '07 #15

P: n/a
On 2007-04-03, Aahz <aa**@pythoncraft.comwrote:
In article <KP******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>>Aahz wrote:
>>In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
Ma***********@gmail.com wrote:
>
I create a dictionary like this
myDict = {}
>
and I add entry like this:
myDict['a'] = 1
but how can I empty the whole dictionary?
just point myDict to an empty dictionary again

myDict={}

Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old programming
thinking that variables "contain" things. As I'm sure you kno,
variables point to things in Python. I don't believe that there are
lots of other objects pointing to this dictionary. Perhaps the OP
can clarify for us. If there aren't other objects pointing to this
dictionary it would make NO sense to iterate over a dictionary and
delete all the keys/values so I tried to read between the lines and
answer what I believe the OP thought he was asking.

Then you should explain why you didn't answer the question that was
asked. Answering a different question without explanation makes your
answer irrelevant at best, wrong at worst.
This is not true. If this different question was in fact the intended
question instead of the one actually asked. Anwering this different
question can be more usefull than answering the one actually asked.

People are often enough not very exact in their communication and
that goes double for people who are new in a particular subject.
So I think it is entirely appropiate to think about the real question
the person is strugling with that hides between the question
actually asked.

Yes sometimes those who try to guess the intentions of the OP
are going totally off in the wrong direction. But I have also
seen those people providing very helpfull information, while
those that would stick to the "actual" question didn'y provide
very usefull information.

--
Antoon Pardon
Apr 4 '07 #16

P: n/a
In article <sl********************@rcpc42.vub.ac.be>,
Antoon Pardon <ap*****@forel.vub.ac.bewrote:
>On 2007-04-03, Aahz <aa**@pythoncraft.comwrote:
>In article <KP******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>>>Aahz wrote:
In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
Ma***********@gmail.com wrote:
>>
>I create a dictionary like this
>myDict = {}
>>
>and I add entry like this:
>myDict['a'] = 1
>but how can I empty the whole dictionary?
just point myDict to an empty dictionary again
>
myDict={}

Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old programming
thinking that variables "contain" things. As I'm sure you kno,
variables point to things in Python. I don't believe that there are
lots of other objects pointing to this dictionary. Perhaps the OP
can clarify for us. If there aren't other objects pointing to this
dictionary it would make NO sense to iterate over a dictionary and
delete all the keys/values so I tried to read between the lines and
answer what I believe the OP thought he was asking.

Then you should explain why you didn't answer the question that was
asked. Answering a different question without explanation makes your
answer irrelevant at best, wrong at worst.

This is not true. If this different question was in fact the intended
question instead of the one actually asked. Anwering this different
question can be more usefull than answering the one actually asked.
Note carefully that I did not say, "Don't answer the question you think
should have been asked." What I said was, "If you answer a different
question, EXPLAIN WHY." Is that so difficult to understand?
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
Apr 5 '07 #17

P: n/a
Tempest in a teapot guys.

Aahz wrote:
In article <sl********************@rcpc42.vub.ac.be>,
Antoon Pardon <ap*****@forel.vub.ac.bewrote:
>On 2007-04-03, Aahz <aa**@pythoncraft.comwrote:
>>In article <KP******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:

Aahz wrote:

In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>
>Ma***********@gmail.com wrote:
>>
>>I create a dictionary like this
>>myDict = {}
>>>
>>and I add entry like this:
>>myDict['a'] = 1
>>but how can I empty the whole dictionary?
>>>
>just point myDict to an empty dictionary again
>>
>myDict={}
>>
Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().
>
I believe he (as many new to Python do) are mired in old programming
thinking that variables "contain" things. As I'm sure you kno,
variables point to things in Python. I don't believe that there are
lots of other objects pointing to this dictionary. Perhaps the OP
can clarify for us. If there aren't other objects pointing to this
dictionary it would make NO sense to iterate over a dictionary and
delete all the keys/values so I tried to read between the lines and
answer what I believe the OP thought he was asking.

Then you should explain why you didn't answer the question that was
asked. Answering a different question without explanation makes your
answer irrelevant at best, wrong at worst.
This is not true. If this different question was in fact the intended
question instead of the one actually asked. Anwering this different
question can be more usefull than answering the one actually asked.

Note carefully that I did not say, "Don't answer the question you think
should have been asked." What I said was, "If you answer a different
question, EXPLAIN WHY." Is that so difficult to understand?
Apr 5 '07 #18

P: n/a
On 2007-04-04, Aahz <aa**@pythoncraft.comwrote:
In article <sl********************@rcpc42.vub.ac.be>,
Antoon Pardon <ap*****@forel.vub.ac.bewrote:
>>On 2007-04-03, Aahz <aa**@pythoncraft.comwrote:
>>In article <KP******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
Aahz wrote:
In article <4_******************************@comcast.com>,
Larry Bates <lb****@websafe.comwrote:
>Ma***********@gmail.com wrote:
>>>
>>I create a dictionary like this
>>myDict = {}
>>>
>>and I add entry like this:
>>myDict['a'] = 1
>>but how can I empty the whole dictionary?
>just point myDict to an empty dictionary again
>>
>myDict={}
>
Go back and read Christian's post, then post a followup explaning why his
solution is better than yours. Your explanation should use id().

I believe he (as many new to Python do) are mired in old programming
thinking that variables "contain" things. As I'm sure you kno,
variables point to things in Python. I don't believe that there are
lots of other objects pointing to this dictionary. Perhaps the OP
can clarify for us. If there aren't other objects pointing to this
dictionary it would make NO sense to iterate over a dictionary and
delete all the keys/values so I tried to read between the lines and
answer what I believe the OP thought he was asking.

Then you should explain why you didn't answer the question that was
asked. Answering a different question without explanation makes your
answer irrelevant at best, wrong at worst.

This is not true. If this different question was in fact the intended
question instead of the one actually asked. Anwering this different
question can be more usefull than answering the one actually asked.

Note carefully that I did not say, "Don't answer the question you think
should have been asked." What I said was, "If you answer a different
question, EXPLAIN WHY." Is that so difficult to understand?
You are mixing up two things:

On the one hand you are trying to get some moral behaviour accrosss:
People should explain when answering a different question.

On the second hand you are bringing an opinion: If they don't their
answer is irrelevant at best.

If someone disagrees with the second, repeating the first seems
a bit beside the point.

--
Antoon Pardon
Apr 10 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.