Deleting dictionary items

Remco Gerlich scarblac at
Mon Mar 19 09:59:19 CET 2001

Terry Reedy <tjreedy at> wrote in comp.lang.python:
> > This makes me think, would it be a sane way of doing things to use a list
> > comprehension to modify a dictionary?
> ...
> > filter(dict, lambda key: key[attr] == value)
> >
> > Is this possible? Desireable? Too stupid to contemplate?
> Funny coincidence.  Just 3 miniute ago, before reading this, I tossed out a
> similar idea, in the thread Subsetting a Dictionary, of adding 'dict
> comprehensions' with syntax directly paralleling list comprehensions.
> {k,v for k,v in dict if condition(k,v)}
> Lists and dicts are subcategories of collection or set.  Subseting is, by
> axiom, a general set operation not limited to ordered sets (lists).  So
> this is hardly a stupid idea.

I'd like 

{ k:v for k,v in dict.items() if condition(k.v) }

better, as long as the "for k,v in dict:" loop isn't going to be in the
language. If it is, then the items() isn't necessary. And I like : better
than ,.

Remco Gerlich

More information about the Python-list mailing list