On Thu, Oct 17, 2013, at 11:31 AM, Sam Lai 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
The funcy library is worth a look as it provides a number of functions
to help with situations like this, as well as providing functions that
programmers coming from other languages (such as Haskell or Clojure) are
used to:
http://hackflow.com/blog/2013/10/13/functional-python-made-easy/
eg
In [1]: from funcy import merge
In [2]: d1 = {1:1,2:4,3:9}
In [3]: d2 = {4:16,5:25}
In [4]: d3 = merge(d1,d2)
In [5]: d3
Out[5]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Regards
Graeme
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug