Removing dictionary-keys not in a set?
Oren Tirosh
oren at REMOVETHIS1.hishome.net
Mon Apr 18 14:39:48 EDT 2005
"Tim N. van der Leeuw" <tim.leeuwvander at nl.unisys.com> wrote in message news:<1113824946.525441.285760 at l41g2000cwc.googlegroups.com>...
> Hi,
>
> I'd like to remove keys from a dictionary, which are not found in a
> specific set.
Here's my magic English-to-Python translator:
"I'd like to ... keys which ..." -> "for key in"
"keys from a dictionary" -> "set(dictionary)"
"not found in a specific set" -> "-specificset"
"... remove keys ..." -> "del dictionary[key]"
Putting it all together:
>>> for key in set(dictionary)-specificset:
... del dictionary[key]
Oren
More information about the Python-list
mailing list