I'd like to avoid the update() method and I read that is possible to merge two dictionaries together into a third dictionary using the "+" operand, but what happens in my shell is this:
>>> {'a':1, 'b':2}.items() + {'x':98, 'y':99}.items()
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
{'a':1, 'b':2}.items() + {'x':98, 'y':99}.items()
TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
>>> {'a':1, 'b':2} + {'x':98, 'y':99}
Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
{'a':1, 'b':2} + {'x':98, 'y':99}
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
How can I get this to work?
or
On Python 3,
items
doesn't return alist
like in Python 2, but a dict view. If you want to use+
, you need to convert them tolist
s.You're better off using
update
with or withoutcopy
: