Why no '|' operator for dict?
On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman <frank at chagford.com> wrote:
> So I have 2 questions -
> 1. Is there any particular reason why '|' is not supported?
'|' is the set union operation, roughly equivalent to the set.union
method. Dicts don't have a union operation. If they did, and the same
key were found in both sets, what would be the value of that key in
> 2. Is there a better way to do what I want?
The dict.items() view is explicitly set-like and can be unioned, so
you can do this:
py> dict(d1.items() | d2.items())
As to the question of which value will appear in the union in the case
of duplicate keys, it will be whichever one arbitrarily appears later
in the iteration order of the intermediate set.