[Tutor] Remove a dictionary entry

bob gailer bgailer at gmail.com
Fri Sep 17 23:01:40 CEST 2010

  On 9/17/2010 9:08 AM, M. 427 wrote:
> Hello,
> (I am very new to python)
> I built a dictionary d={} of lists similar to this :
> d = {
> 'a': ['apricot', 'apple'],
> 'b': ['beach', 'bear', 'bottle'],
> 'c': ['cold', 'cook', 'coleslaw'],
> 'd': ['deep'],
> 'e': ['expression', 'elephant']
> }
> Now i want to go through this dictionary and remove all rows containing
> only one entry. How should I do that?

We like to help when you tell us what part(s) of the answer you know. 
There are 4 things involved here:
1 - How to get a list of all the key-value pairs in a dictionary.
2 - How to loop thru that list.
3 - How to get the length of a value's list.
4 - How to test that for equal to 1.
5- How to delete a dictionary entry knowing the key.

Which of those do you know? Which do you need help with?
> Question 2 : where should I have found this answer myself? (apart from
> here)
It is hard to give a specific answer since we don't know what you don't 

I suspect most tutorials will give you the answers.

In the Python Reference /6.8 Mapping Type - dict /it tells you the 
answers to 1 and 5.

In the Python Reference /6.6.4. Mutable Sequence Types/ it tells you the 
answer to 3.

In the Python Reference /5.9 Comparisons/ it tells you the answer to 4.

Regarding 3 there are several ways to loop through a sequence. Tutorials 
explain these.

Bob Gailer
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100917/3d98da51/attachment.html>

More information about the Tutor mailing list