how can I clear a dictionary in python

Larry Bates lbates at websafe.com
Thu Mar 29 09:16:26 EDT 2007


Aahz wrote:
> In article <4_SdndYI-pFUbZfbnZ2dnUVZ_hzinZ2d at comcast.com>,
> Larry Bates  <lbates at websafe.com> wrote:
>> Marko.Cain.23 at 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



More information about the Python-list mailing list