0

I have the following Python script where I merge two dictionaries:

dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}
dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}
print dict2.update(dict1)

Why do I get as output None rather than the merged dictionaries? How can I display the result?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385

2 Answers2

2

update does not return a new dictionary. Do this instead:

dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}
dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}
dict2.update(dict1)
print(dict2)
1

dict2.update(dict1) updates dict2, but doesn't return it. Use print dict2 instead.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159