[Tutor] Remove a dictionary entry

bob gailer bgailer at gmail.com
Sat Sep 18 03:36:17 CEST 2010

  On 9/17/2010 9:21 PM, M. 427 wrote:
> Thank you,
> After reading the following documentations
> http://docs.python.org/tutorial/datastructures.html#looping-techniques
> http://docs.python.org/tutorial/controlflow.html#for-statements
> I ended up with this :
> Version 3 :
> for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type
>      if len(row)<  2 :
>          del d[i]
> Still buggy... Any lead for this error message? Is a slice unhashable?
> Am I looking in the right direction for this task?
Where did you see [:] after a dict? [:] is slicing, and applies to a 
sequence not a mapping.

Also note the warning "Using iteritems()  while adding or deleting 
entries in the dictionary may raise a RuntimeError  or fail to iterate 
over all entries."

You should get a list rather than an iterator of all key-value pairs 
then iterate over that list.

Bob Gailer
Chapel Hill NC

More information about the Tutor mailing list