osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why no '|' operator for dict?


"Ian Kelly"  wrote in message 
news:CALwzidkP3Ls4s-Zi3ax6no-68kw4_XDOzvWA-cJ+oz+ApqrWXg at mail.gmail.com...
>
> 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())
>

Excellent explanation, and excellent solution!

Thanks very much.

Frank