Skip to content
Advertisement

Why is arithmetic not supported for dict? Usupported operand type(s) for +: ‘dict’ and ‘dict’

In Python, there is a possibility to sum lists and tuples, e.g.

JavaScript

But trying to do the same with dicts will raise:

JavaScript

I guess there could be the same behavior as update() has for cases when merging two dicts with the same key:

JavaScript

Why these operands aren’t implemented? Any optimization issues or just by design?

Advertisement

Answer

In Python 3.9 was added dict union operator, for example:

JavaScript

Also, take a look at motivation, it has a good overview of why this was included in the language.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement