[Python-ideas] Add a method to get the subset of a dictionnary.

Enguerrand Pelletier enguerrand.pelletier at gmail.com
Wed Oct 12 12:06:51 EDT 2016


Hi all,

It always bothered me to write something like this when i want to strip 
keys from a dictionnary in Python:

a = {"foo": 1, "bar": 2, "baz": 3, "foobar": 42}
interesting_keys = ["foo", "bar", "baz"]
b = {k, v for k,v in a.items() if k in interesting_keys}

Wouldn't it be nice to have a syntactic sugar such as:

b = a.subset(interesting_keys)

I find this version more elegant/explicit. But maybe this feature is not 
"worth it"

Cheers !


More information about the Python-ideas mailing list