Re: Why no '|' operator for dict?

2018-02-05 Thread Andre Müller
You can use keyword-argument unpacking in a dict-constructor. Values of duplicate keys are overwritten from left to right. The last wins. >>> dict1 = {'foo': 13, 'bar': 42} >>> dict2 = {'foo': 42, 'hello': 'world'} >>> {**dict1, **dict2} {'bar': 42, 'foo': 42, 'hello': 'world'} {**dict2, **dict1

Re: Why no '|' operator for dict?

2018-02-05 Thread Steven D'Aprano
On Mon, 05 Feb 2018 01:14:53 -0700, Ian Kelly wrote: > On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman > 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'

Re: Why no '|' operator for dict?

2018-02-05 Thread Serhiy Storchaka
05.02.18 10:14, Ian Kelly пише: On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman 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 di

Re: Why no '|' operator for dict?

2018-02-05 Thread Terry Reedy
On 2/5/2018 2:35 AM, Frank Millman wrote: I recently learned that you can create a set 'on-the-fly' from two existing sets using the '|' operator - Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license"

Re: Why no '|' operator for dict?

2018-02-05 Thread Maxime S
2018-02-05 9:14 GMT+01:00 Ian Kelly : > On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman wrote: >> 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 wh

Re: Why no '|' operator for dict?

2018-02-05 Thread Frank Millman
"Ian Kelly" wrote in message news:calwzidkp3ls4s-zi3ax6no-68kw4_xdozvwa-cj+oz+apqr...@mail.gmail.com... On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman wrote: > So I have 2 questions - > > 1. Is there any particular reason why '|' is not supported? '|' is the set union operation, roughly equiv

Re: Why no '|' operator for dict?

2018-02-05 Thread Ian Kelly
On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman 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

Why no '|' operator for dict?

2018-02-04 Thread Frank Millman
Hi all I recently learned that you can create a set 'on-the-fly' from two existing sets using the '|' operator - Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. set_1 = set