Is there an Object spread syntax in python 2.7x like in Javascript?

If you had Python >=3.5, you can use key-word expansion in dict literal:

>>> d = {'x': '2', 'y': '1'}
>>> {**d, 'x':1}
{'x': 1, 'y': '1'}

This is sometimes referred to as “splatting”.

If you are on Python 2.7, well, there is no equivalent. That’s the problem with using something that is over 7 years old. You’ll have to do something like:

>>> d = {'x': '2', 'y': '1'}
>>> x = {'x':1}
>>> x.update(d)
>>> x
{'x': '2', 'y': '1'}

Leave a Comment