Set union (| operator) is commutative.

>>> {2,3} | {1,2} == {1,2} | {2,3}
True

List concatenation isn't

>>> [2,3] + [1,2] == [1,2] + [2,3]
False

so maybe the expectation of commutativity for + isn't a good argument (or is an 
argument that list concatenation should be called something else :) ).

If you want a (verbose) one-liner for "concatenation" of dictionaries, there's 
always:

dict(itertools.chain({1:1}.iteritems(), {2:2}.iteritems()))

On 17/10/2013, at 11:31 AM, Sam Lai <[email protected]> wrote:

> It's almost Friday, so I have a question where I'm pretty sure I'm
> missing something obvious.
> 
> Given,
> 
> d1 = { 'a' : 'b' }
> d2 = { 'c' : 'd' }
> 
> ...  why isn't d3 = d1 + d2 implemented to be equivalent to -
> 
> d3 = { }
> d3.update(d1)
> d3.update(d2)
> 
> It doesn't work for sets either, but it works in this fashion for
> lists. Is this because the operation is non-commutative for sets and
> dicts and may result in a loss of data when clashing keys are
> involved? Isn't that implicit when working with sets and dicts?
> 
> Sam
> _______________________________________________
> melbourne-pug mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/melbourne-pug

_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug

Reply via email to