osdir.com


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

Why no '|' operator for dict?


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" for more information.
>>>>
>>>> set_1 = set(('a', 'b', 'c'))
>>>> set_2 = set(('d',))
>>>> set_1 | set_2
> {'d', 'a', 'c', 'b'}

> I was hoping that I could do the same with a dictionary, but it does not 
> work -

If you know keys are unique or you want keep the second value for a 
duplicate (equal) key,

 >>> d1 = {1: 'one', 2: 'two'}; d2 = {3: 'three', 1:'One'}
 >>> d3 = d1.copy()
 >>> d3.update(d2)
 >>> d3
{1: 'One', 2: 'two', 3: 'three'}


-- 
Terry Jan Reedy