[Python-ideas] Joining dicts again

Ryan Gonzalez rymg19 at gmail.com
Fri Feb 21 17:10:48 CET 2014


...but that doesn't work if you have non-string keys.

http://stackoverflow.com/a/39858/2097780



On Fri, Feb 21, 2014 at 4:15 AM, Chris Angelico <rosuav at gmail.com> wrote:

> On Fri, Feb 21, 2014 at 8:40 PM,  <haael at interia.pl> wrote:
> > I know this has been mangled thousand times, but let's do it once again.
> >
> > Why does Python not have a simple dict joining operator?
> >
> > From what I read, it seems the biggest concern is: which value to pick
> up if both dicts have the same key.
> > a = {'x':1}
> > b = {'x':2}
> > c = a | b
> > print(c['x']) # 1 or 2?
>
> If you can pick one of the dicts to "win", then you can use this notation:
>
> c = dict(a, **b)
>
> Anything in b will override a. It's short and a single expression
> (unlike "c = a.copy(); c.update(b)"). It's not perfectly clear what's
> happening, though, so it may warrant a comment.
>
> ChrisA
> _______________________________________________
> 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/
>



-- 
Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple:
"It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was
nul-terminated."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140221/22ed5ed1/attachment-0001.html>


More information about the Python-ideas mailing list