[Python-ideas] Syntax for allowing extra keys when unpacking a dict as keyword arguments

Viktor Roytman viktor.roytman at gmail.com
Sat Apr 13 12:34:53 EDT 2019


On Saturday, April 13, 2019 at 9:03:54 AM UTC-4, Chris Angelico wrote:
>
> On Sat, Apr 13, 2019 at 11:00 PM Juancarlo AƱez <apa... at gmail.com 
> <javascript:>> wrote:
> > func(**{k:v for k, v in d.items() if k in ('a','b','c'))
> >
>
> Would be really nice to be able to spell this as a dict/set intersection.
>
> func(**(d & {'a', 'b', 'c'}))
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python... at python.org <javascript:>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
I like this idea. It accomplishes the goal of only using some of the keys 
and has broader applications for working with dicts in general.

It might also be nice to have something that splits a dict into two, like

    >>> items = dict(a=1, b=2, c=3)
    >>> included, excluded = items &- {'a', 'b'}
    >>> print(included)
    {'a': 1, 'b': 2}
    >>> print(excluded)
    {'c': 3}

I don't know if I like the &- "operator" but it is illustrative.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190413/3695437c/attachment.html>


More information about the Python-ideas mailing list