[Tutor] Deleting an entry from a dictionary

Smith, Jeff jsmith at medplus.com
Wed Aug 3 14:52:25 CEST 2005


Although that works, I kinda prefer
    del meals['breakfast']
since that explicitly indicates what is going on.
 
Speaking of which, I note that there is a pop for lists but no shift.
Is there a Python idiom for this or is it just
    val = mylist.shift() =>    (val, mylist) = (mylist[0], mylist[1:])
which seems a little clumsy.
 
Jeff
 
 
 
-----Original Message-----
From: tutor-bounces+jsmith=medplus.com at python.org
[mailto:tutor-bounces+jsmith=medplus.com at python.org] On Behalf Of Adam
Bark
Sent: Tuesday, August 02, 2005 5:17 PM
To: Greg Lindstrom
Cc: tutor at python.org
Subject: Re: [Tutor] Deleting an entry from a dictionary



meals.pop(key) will do it.
Example:
>>> meals = {}
>>> meals['breakfast'] = 'slimfast'
>>> meals['lunch'] = 'slimfast'
>>> meals['dinner'] = 'something sensible'
>>> meals
{'lunch': 'slimfast', 'breakfast': 'slimfast', 'dinner': 'something
sensible'}
>>> meals.pop("breakfast")
'slimfast'
>>> meals
{'lunch': 'slimfast', 'dinner': 'something sensible'}


On 8/2/05, Greg Lindstrom <greg.lindstrom at novasyshealth.com
<mailto:greg.lindstrom at novasyshealth.com> > wrote: 

Hello-
This must be simple, but for the life of me I can't figure out how to 
delete an entry from a dictionary.  For example,

meals = {}
meals['breakfast'] = 'slimfast'
meals['lunch'] = 'slimfast'
meals['dinner'] = 'something sensible'

How do I eliminate 'lunch' from the dictionary so that I only have 
'breakfast' and 'dinner'?

Thanks!
--greg

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050803/9e3db575/attachment.htm


More information about the Tutor mailing list