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(('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 -

dict_1 = {1: 'one', 2: 'two'}
dict_2 = {3: 'three'}
dict_1 | dict_2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict' and 'dict'


The best that I can come up with is -

dict([(k, v) for k, v in dict_1.items()] + [(k, v) for k, v in dict_2.items()])
{1: 'one', 2: 'two', 3: 'three'}


So I have 2 questions -

1. Is there any particular reason why '|' is not supported?

2. Is there a better way to do what I want?

Thanks

Frank Millman


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to