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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Adam Bark
Sent: Tuesday, August 02, 2005 5:17 PM
To: Greg Lindstrom
Cc: tutor@python.org
Subject: Re: [Tutor] Deleting an entry from a dictionary
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Adam Bark
Sent: Tuesday, August 02, 2005 5:17 PM
To: Greg Lindstrom
Cc: tutor@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 <[EMAIL PROTECTED] > 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@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor