<div dir="ltr">I mentioned this on the python-dev list [1] originally as a +1 to someone else suggesting the idea [2]. It also came up in a response to my post that I can't seem to find in the archives, so I've quoted it below [3].<div><br></div><div>As the subject says, the idea would be to add a "+" and "+=" operator to dict that would provide the following behavior:</div><br>>>> {'x': 1, 'y': 2} + {'z': 3}<br>{'x': 1, 'y': 2, 'z': 3}<div><br></div><div>With the only potentially non obvious case I can see then is when there are duplicate keys, in which case the syntax could just be defined that last setter wins, e.g.:<br><br>>>> {'x': 1, 'y': 2} + {'x': 3}<br>{'x': 3, 'y': 2}<br><br>Which is analogous to the example:<br><br>>>> new_dict = dict1.copy()<br>>>> new_dict.update(dict2)</div><div><br></div><div>With "+=" then essentially ending up being an alias for ``dict.update(...)``.</div><div><br></div><div>I'd be happy to champion this as a PEP if the feedback / public opinion heads in that direction. </div><div><br><br><div><div>[1] <a href="https://mail.python.org/pipermail/python-dev/2015-February/138150.html">https://mail.python.org/pipermail/python-dev/2015-February/138150.html</a></div><div>[2] <a href="https://mail.python.org/pipermail/python-dev/2015-February/138116.html">https://mail.python.org/pipermail/python-dev/2015-February/138116.html</a><div><div class="gmail_signature">[3] John Wong -- </div><blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex" class="gmail_quote"><span style="font-size:12.8000001907349px">Well looking at just list<br></span>a + b yields new list<br>a += b yields modified a<br>then there is also .extend in list. etc. <br>so do we want to follow list's footstep? I like + because + is more natural to read. Maybe this needs to be a separate thread. I am actually amazed to remember dict + dict is not possible... there must be a reason (performance??) for this...</blockquote><div class="gmail_signature"><br></div><div class="gmail_signature">Cheers,</div><div class="gmail_signature"><br>~ Ian Lee<br></div></div>
</div></div></div></div>