[Python-ideas] dict.items to accept optional iterable with keys to use

Chris Rebert pyideas at rebertia.com
Wed Apr 4 10:22:05 CEST 2012


On Wed, Apr 4, 2012 at 1:07 AM, Victor Varvariuc
<victor.varvariuc at gmail.com> wrote:
> Sometimes you want a dict which is subset of another dict. It would nice if
> dict.items accepted an optional list of keys to return. If no keys are given
> - use default behavior - get all items.
<snip>
> print(dict(a.items((1, 3, 5))))

In that use case, why not just write a dict comprehension?:
    print({k: a[k] for k in (1, 3, 5)})
Completely explicit, and only a mere few characters longer.

Cheers,
Chris



More information about the Python-ideas mailing list