[Tutor] Deleting an entry from a dictionary

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Aug 2 23:12:23 CEST 2005



On Tue, 2 Aug 2005, Greg Lindstrom wrote:

> 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'?


Hi Greg,

Actually, it isn't obvious at all, so don't be too discouraged.  The 'del'
statement should do the trick:

######
>>> d = {'a': 'Alpha', 'b' : 'Beta'}
>>> del d['a']
>>> d
{'b': 'Beta'}
######


Here's a reference list of all the things you can do to a dictionary:

    http://www.python.org/doc/lib/typesmapping.html


Good luck!



More information about the Tutor mailing list