[Tutor] Deleting an entry from a dictionary

Adam Bark adam.jtm30 at gmail.com
Wed Aug 3 16:02:03 CEST 2005


Oh yeah using del is better. I was typing as Danny sent his reply so I 
didn't realise untill after I had sent that.

On 8/3/05, Smith, Jeff <jsmith at medplus.com> wrote:
> 
> 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 > 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/2b3e8bf6/attachment-0001.htm


More information about the Tutor mailing list