0

Imagine I have

dict1 = {'uno':[1,2,3],'dos':[4,5,6]}

and

dictA = {'AA':'ZZZZZ'}

This works:

dict1.update(dictA)

Result: {'uno': [1, 2, 3], 'dos': [4, 5, 6], 'AA':'ZZZZZ'}

But this does not work:

B = dict1.update(dictA)

The result is not an error but Result is None, which makes this behaviour (IMMO) strange and dangerous since the code does not crash.

So Why is returning None and not giving error?

Note: It looks like the way to go is:

C = dict1.update(dictA)
B = {}
B.update(dict1)
B.update(dictA)
B

C is none B is OK here

Neuron
  • 5,141
  • 5
  • 38
  • 59
JFerro
  • 3,203
  • 7
  • 35
  • 88
  • `update` function updates current `dict` and return `None` so value stored in `B` will be None. – Sociopath Sep 08 '20 at 10:44
  • It is not strange, it updates the dictionary in-place. Hence returns `None` – yatu Sep 08 '20 at 10:45
  • Thanks, checking the other questions associated I dont find a reason why this returns None. I mean a use case or so. Perhaps it is a philosophical question. Its documented, we know the way around it, but the question is why is so? – JFerro Sep 08 '20 at 10:52

1 Answers1

1

When using update it update dict1 the dictionary given as parameter and returns None:

Docs:

dict. update([mapping]) mapping Required. Either another dictionary object or an iterable of key:value pairs > (iterables of length two). If keyword arguments are specified, the dictionary is > then updated with those key:value pairs. Return Value None

Code:

dict1 = {'uno':[1,2,3],'dos':[4,5,6]}

dict1.update({'tres':[7,8,9]})

# {'uno': [1, 2, 3], 'dos': [4, 5, 6], 'tres': [7, 8, 9]}
print(dict1)
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22