[Tutor] omit some keys from dict

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 7 Feb 2002 19:42:29 +0100

On  0, Karthik Gurumurthy <karthikg@aztec.soft.net> wrote:
> I have a dict and i don't want to look for certain keys.
> "omit" lists the keys i want to omit.
> To get the remaining keys i can probably do this..
> j= {1:'hello',2:'fine',3:'python',4:'dicts',5:'lists',6:'tuples'} #actual
> dict
> omit = [1,3,5]
> checkkeys = [key for key in j.keys() if key not in omit]
> Now how can i write this using *only* filter and lambda?

checkkeys = filter(lambda key: key not in omit, j.keys())

> I thought i w'd'nt use list comprehensions, but now for this problem
> i had to use LCs.

Nah :-)

Things that start with [x for x in ... if] basically *are* filters.

Remco Gerlich