[Python-ideas] Accepting multiple mappings as positional arguments to create dicts

Ed Kellett e+python-ideas at kellett.im
Thu Apr 12 10:34:00 EDT 2018


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}

Ed

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180412/154b1128/attachment.sig>


More information about the Python-ideas mailing list