Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how can I clear a dictionary in python

0 views
Skip to first unread message

Marko....@gmail.com

unread,
Mar 28, 2007, 6:38:45 PM3/28/07
to
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.

Christian Tismer

unread,
Mar 28, 2007, 6:47:53 PM3/28/07
to Marko....@gmail.com, pytho...@python.org

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

Larry Bates

unread,
Mar 28, 2007, 6:48:46 PM3/28/07
to

just point myDict to an empty dictionary again

myDict={}

Larry Bates

Bruno Desthuilliers

unread,
Mar 28, 2007, 7:27:30 PM3/28/07
to
Marko....@gmail.com a écrit :

>>> 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
{}
>>>

Aahz

unread,
Mar 28, 2007, 6:51:58 PM3/28/07
to
In article <4_SdndYI-pFUbZfb...@comcast.com>,
Larry Bates <lba...@websafe.com> wrote:

>Marko....@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!

Christian Tismer

unread,
Mar 28, 2007, 7:02:28 PM3/28/07
to Larry Bates, pytho...@python.org

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"""

Bjoern Schliessmann

unread,
Mar 28, 2007, 7:51:07 PM3/28/07
to
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

Aahz

unread,
Mar 28, 2007, 7:58:15 PM3/28/07
to
In article <570dbbF...@mid.individual.net>,

Bjoern Schliessmann <usenet-mail-03...@spamgourmet.com> 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 ...

We don't. However, we do know exactly what the OP said... (Perhaps you
didn't read the Subject: line?)

Steven D'Aprano

unread,
Mar 29, 2007, 12:56:44 AM3/29/07
to
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.


Larry Bates

unread,
Mar 29, 2007, 9:16:26 AM3/29/07
to
Aahz wrote:
> In article <4_SdndYI-pFUbZfb...@comcast.com>,
> Larry Bates <lba...@websafe.com> wrote:
>> Marko....@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

Bruno Desthuilliers

unread,
Mar 29, 2007, 10:38:34 AM3/29/07
to
Larry Bates a écrit :

> Aahz wrote:
>> In article <4_SdndYI-pFUbZfb...@comcast.com>,
>> Larry Bates <lba...@websafe.com> wrote:
>>> Marko....@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.

Larry Bates

unread,
Mar 29, 2007, 12:40:05 PM3/29/07
to Bruno Desthuilliers

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


Russ

unread,
Mar 29, 2007, 9:10:38 PM3/29/07
to
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?

Alex Martelli

unread,
Mar 29, 2007, 10:14:26 PM3/29/07
to
Russ <uymq...@sneakemail.com> wrote:

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

Aahz

unread,
Apr 3, 2007, 3:58:32 PM4/3/07
to
In article <KP-dnXOp7ea8IZbb...@comcast.com>,

Larry Bates <lba...@websafe.com> wrote:
>Aahz wrote:
>> In article <4_SdndYI-pFUbZfb...@comcast.com>,
>> Larry Bates <lba...@websafe.com> wrote:
>>> Marko....@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.

Antoon Pardon

unread,
Apr 4, 2007, 4:12:55 AM4/4/07
to

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

Delaney, Timothy (Tim)

unread,
Apr 4, 2007, 7:35:45 PM4/4/07
to pytho...@python.org
Antoon Pardon wrote:

> 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.

And yet it's giving the *wrong* information if the person was asking
exactly the question they intended to ask.

If you think there is some ambiguity, you should *clarify* the question
before answering. Answering a different question to the one that is
asked just causes problems, and the kind of thread this has degenerated
into.

Tim Delaney

Aahz

unread,
Apr 4, 2007, 7:59:15 PM4/4/07
to
In article <slrnf16ng7....@rcpc42.vub.ac.be>,

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?

Why is this newsgroup different from all other newsgroups?

Steven Howe

unread,
Apr 4, 2007, 8:19:35 PM4/4/07
to Aahz, pytho...@python.org
Tempest in a teapot guys.

Antoon Pardon

unread,
Apr 10, 2007, 12:26:51 AM4/10/07
to
On 2007-04-04, Delaney, Timothy (Tim) <tdel...@avaya.com> wrote:
> Antoon Pardon wrote:
>
>> 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.
>
> And yet it's giving the *wrong* information if the person was asking
> exactly the question they intended to ask.

So? You are in a sitituation where it isn't clear what answer will
be the most usefull. So you make a judgement call and judgment calls
can sometimes be wrong.

> If you think there is some ambiguity, you should *clarify* the question
> before answering. Answering a different question to the one that is
> asked just causes problems, and the kind of thread this has degenerated
> into.

I disagree. This thread has degenerated, not because some people
answered a different question, but because other people didn't
like that and reacted. If you think an answer to a particular
question needs extra clarification, you can just provide that
clarification. Of course some people can always find complains.
Even if you point out errors in what other say, you can draw complaints
because they think you are bothering with details they think are
unimportant which may detract from what they think is the real issue.

So my advise is not to bother with telling how questions should be
answered but to just provide the information you think is missing.

--
Antoon Pardon

Antoon Pardon

unread,
Apr 10, 2007, 1:59:28 AM4/10/07
to

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

0 new messages