Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how can I clear a dictionary in python

Reply
Thread Tools

how can I clear a dictionary in python

 
 
Marko.Cain.23@gmail.com
Guest
Posts: n/a
 
      03-28-2007
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.

 
Reply With Quote
 
 
 
 
Christian Tismer
Guest
Posts: n/a
 
      03-28-2007

On 29.03.2007, at 00:38, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      03-28-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      03-28-2007
In article <(E-Mail Removed)>,
Larry Bates <(E-Mail Removed)> wrote:
>(E-Mail 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().
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Need a book? Use your library!
 
Reply With Quote
 
Christian Tismer
Guest
Posts: n/a
 
      03-28-2007

On 29.03.2007, at 00:48, Larry Bates wrote:

> (E-Mail Removed) 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"""
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-28-2007
(E-Mail 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(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

{}
>>>


 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      03-28-2007
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

 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      03-28-2007
In article <(E-Mail Removed)>,
Bjoern Schliessmann <(E-Mail Removed)> 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?)
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Need a book? Use your library!
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-29-2007
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.


 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      03-29-2007
Aahz wrote:
> In article <(E-Mail Removed)>,
> Larry Bates <(E-Mail Removed)> wrote:
>> (E-Mail 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. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM



Advertisments