Why no '|' operator for dict?
05.02.18 10:14, Ian Kelly ????:
> 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
> the union?
>> 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())
This doesn't work with non-hashable values.
The simplest (and perhaps the most efficient) way in recent Python
In old Python versions this should be written as
d = dict(d1)