Deleting dictionary items

Terry Reedy tjreedy at udel.edu
Sun Mar 18 22:16:31 EST 2001


> 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.

Terry J. Reedy






More information about the Python-list mailing list