0

How do you spread a dict in python?

The ES6 JS code I need to replicate is:

const geo = {'lat': 123, 'lon': 456};
const time = "2021-05-30T13:18:03+00:00"

const spreadedObject = {...geo, time: time}

console.log(spreadedObject);

{ lat: 123, lon: 456, time: '2021-05-30T13:18:03+00:00' }

There must be something like spread in python, right?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
wzr1337
  • 3,609
  • 5
  • 30
  • 53
  • 1
    Spread Operator in Python `*` – Shri Hari L May 30 '21 at 13:22
  • 1
    `dict(**geo)` would make a shallow copy, and you can add arbitrary other keyword arguments to it. See https://stackoverflow.com/q/47875815/3001761. – jonrsharpe May 30 '21 at 13:22
  • 1
    If your question is about Python, don't tag JavaScript. Just because your question *mentions* JavaScript, that doesn't mean it should be in the JavaScript tag. – T.J. Crowder May 30 '21 at 13:31

1 Answers1

0

... of course I found the answer right after posting the question, so I share it for all of you

geo = {"lat":123,"lon":456}
time = "2021-05-30T13:18:03+00:00"
b = {**geo, "time": time}

print(b)

{'lat': 123, 'lon': 456, 'time': '2021-05-30T13:18:03+00:00'}

wzr1337
  • 3,609
  • 5
  • 30
  • 53