-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
  • 9,737
  • 4
  • 53
  • 59
intrigued_66
  • 16,082
  • 51
  • 118
  • 189

2 Answers2

0
both = {}
one = {"A" : 0}
two = {"B" : 0}
both.update(one)
both.update(two)
print one
print both
nishant kumar
  • 507
  • 10
  • 28
-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.