Subsetting a dictionary

Terry Reedy tjreedy at udel.edu
Sun Mar 18 21:56:36 EST 2001


"Will Newton" <will at nospam.misconception.org.uk> wrote in message
news:993dkm$ttr$1 at news6.svr.pol.co.uk...
>
> I need to get a subset of a dictionary:
>
> D = Dictionary of objects (in this case, training examples for a decision
> tree)
>
> D2 = {D | d in D, d under some constraint}

Hmmm.  What you need is a currently imaginary feature that we might call a
dict comprehension,
with syntax paralleling that for list comprehensions:

D2 = {key:val for (key,val) in D if constraint(key,val)}

This idea might be an argument for (or against?) making the phrase 'key,val
in dict' meaningful.

Anyone for a PEP?

Terry J. Reedy






More information about the Python-list mailing list