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
  • 44,640
  • 91
  • 243
  • 375

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
  • 32,558
  • 15
  • 103
  • 148