Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why while updating a dictionary getting None? [duplicate]

enter image description hereI am basically merging two dictionaries by using update method. The problem is when I merge in python shell it works but not in a file while executing.

v = {'customer_id': '9000', 'customer_name': 'Apple  Inc'}
b = {"a": "b"}

print v.update(b)

output of above is None

but its working in shell. What's my silly mistake? Thankyou

like image 483
Nikhil Parmar Avatar asked Apr 01 '16 12:04

Nikhil Parmar


2 Answers

v.update(b) is updating b in place. v is indeed updated, but the result of the update function is None, exactly what is printed out. If you do something like

v.update(b)
print v

you'll see v (updated)

like image 166
Mathias711 Avatar answered Nov 12 '22 08:11

Mathias711


The update function returns None. So

print v.update(b)  # this is printing out the return value of the update function.

To print out the updated value of the dict, just print the dict again

print v  # This will print the updated value of v
like image 36
Saif Asif Avatar answered Nov 12 '22 09:11

Saif Asif