0

I have a function that returns a dict as variable that contains 3 elements with index1, index2, index3 and the corresponded value How can I pass those 3 elements and append to exist dict variable without using an additional variable. All I can think of is using extra variable to pass value.

trans = {}
trans = rtn_func()
want['index1'] = trans['index1']
want['index2'] = trans['index2']
want['index3'] = trans['index3']

print(trans)
{ 'index1': 'value1', 'index2': 'value2', 'index3': 'value3', }
print(want)
{ 'o_inx1': 'nv1', 'o_inx2': 'nv4', 'o_inx3': 'nv1', 'index1': 'value1',
  'index2': 'value2', 'index3': 'value3', }
martineau
  • 119,623
  • 25
  • 170
  • 301
jacobcan118
  • 7,797
  • 12
  • 50
  • 95

1 Answers1

4

you can use update

trans = { 'index1': 'value1', 'index2': 'value2', 'index3': 'value3', }
want = { 'o_inx1': 'nv1', 'o_inx2': 'nv4', 'o_inx3': 'nv1', }
want.update(trans)
user3148949
  • 578
  • 1
  • 9
  • 22