-5

I have the following Python code I am trying to execute but the interpreter is returning prematurely. I believe it is because I am trying to combine two dictionaries as if they were lists:

both = {}
one = {"A" : 0}
two = {"B" : 0}
both = one + two    // Returns prematurely here

How can I combine two dictionaries into one?

Alexander
  • 8,999
  • 3
  • 49
  • 58
mezamorphic
  • 14,525
  • 48
  • 112
  • 173

2 Answers2

0
both = {}
one = {"A" : 0}
two = {"B" : 0}
both.update(one)
both.update(two)
print one
print both
nishant kumar
  • 429
  • 7
  • 26
-3

Have you tried one.update(two)? It will give you the desired combined dict. But you need to be careful as keys in both are overwritten.