[Python-ideas] Dict joining using + and +=

Adrien Ricocotam ricocotam at gmail.com
Fri Mar 1 01:44:57 EST 2019


I really like this idea. It’s not obvious how to deal with key conflicts
and I don’t think replacing by the keys of the second dict is that
obviously a good behaviour. With the actual merging ({**d1, **d2}) it works
the same as when you build a custom dict so it’s usually known by people.
If we add a new syntax/function, we might think of better behaviors.

IMO, and I might be wrong, merging two mapping having common keys is an
error. Thus we would need a clean way to combine two dicts. A simple way
could be adding a key function that takes the values of each merged dict
and returns the new value :

d1 = ...
d2 = ...

d1.merge(d2, key=lambda values: values[0])

That’s an example, I don’t like the syntax.

On Fri 1 Mar 2019 at 07:09, fhsxfhsx <fhsxfhsx at 126.com> wrote:

> Considering potential ambiguity, I suggest `d1.append(d2)` so we can have
> an additional argument saying `d1.append(d2, mode="some mode that tells how
> this function behaviours")`.
> If we are really to have the new syntax `d1 + d2`, I suggest leaving it
> for `d1.append(d2, mode="strict")` which raises an error when there're
> duplicate keys. The semantics is nature and clear when two dicts have no
> overlapping keys.
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190301/e70e8769/attachment-0001.html>


More information about the Python-ideas mailing list