[Python-ideas] Accepting multiple mappings as positional arguments to create dicts
Guido van Rossum
guido at python.org
Thu Apr 12 11:08:09 EDT 2018
On Thu, Apr 12, 2018 at 7:34 AM, Ed Kellett <e+python-ideas at kellett.im>
wrote:
> On 2018-04-12 14:46, Andrés Delfino wrote:
> > Extending the original idea, IMHO it would make sense for the dict
> > constructor to create a new dictionary not only from several mappings,
> but
> > mixing mappings and iterables too.
> >
> > Consider this example:
> >
> > x = [(1, 'one')]
> > y = {2: 'two'}
> >
> > Now: {**dict(x), **y}
> > Proposed: dict(x, y)
> >
> > I think this extension makes the call ostensibly easier to read and grep.
>
> It allows for creating a flattened dict from an iterable of dicts, too,
> which I've occasionally wanted:
>
> >>> configs = {'a': 'yes'}, {'b': 'no'}, {'c': 3}
> >>> dict(*configs)
> {'a': 'yes', 'b': 'no', 'c': 3}
>
> versus:
>
> >>> dict(chain.from_iterable(c.items() for c in configs))
> {'a': 'yes', 'b': 'no', 'c': 3}
Yes, this all sounds totally reasonable.
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180412/1a9e1d31/attachment-0001.html>
More information about the Python-ideas
mailing list