how can I clear a dictionary in python

M

Marko.Cain.23

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

Christian Tismer

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
 
L

Larry Bates

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
 
A

Aahz

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().
 
C

Christian Tismer

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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 on class dict in module __builtin__:

class dict(object)
(...)
| Methods defined here:
(...)
|
| clear(...)
| D.clear() -> None. Remove all items from D.
(...)
 
B

Bjoern Schliessmann

Aahz said:
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
 
A

Aahz

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?)
 
S

Steven D'Aprano

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

Larry Bates

Aahz said:
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
 
B

Bruno Desthuilliers

Larry Bates a écrit :
Aahz said:
(e-mail address removed) 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.
 
L

Larry Bates

Bruno said:
Larry Bates a écrit :
Aahz said:
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
 
R

Russ

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?
 
A

Alex Martelli

Russ said:
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
 
A

Aahz

Aahz said:
(e-mail address removed) 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.
 
A

Antoon Pardon

Aahz said:
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.
 
A

Aahz

Aahz 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?
 
S

Steven Howe

Tempest in a teapot guys.
Antoon Pardon said:
Aahz wrote:

(e-mail address removed) 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?
 
A

Antoon Pardon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top