1

I would like to append to a dictionary for e.g

a={1:2}
a.update({2:4})
a.update({1,3})

I would like this to give 
a ={1: (2,3), 2: 4}

How can this be done? There is no append. Update overwrites this as

 {1: 3, 2: 4}

Please let me know

Tinniam V. Ganesh
  • 1,979
  • 6
  • 26
  • 51

1 Answers1

1

You could try something like this:

a = {1:2}

updates = [{2:4},{1:3}]

for dct in updates:
    for key in dct:
        if key in a:
            a[key] = (a[key], dct[key])
        else:
            a[key] = (dct[key])

print(a)

#Gives: {1: (2, 3), 2: 4}
kjmerf
  • 4,275
  • 3
  • 21
  • 29