Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does dict(k=4, z=2).update(dict(l=1)) return None in Python?

Why does dict(k=4, z=2).update(dict(l=1)) return None? It seems as if it should return dict(k=4, z=2, l=1)? I'm using Python 2.7 should that matter.

like image 421
raster-blaster Avatar asked Aug 03 '13 22:08

raster-blaster


1 Answers

The .update() method alters the dictionary in place and returns None. The dictionary itself is altered, no altered dictionary needs to be returned.

Assign the dictionary first:

a_dict = dict(k=4, z=2)
a_dict.update(dict(l=1))
print a_dict

This is clearly documented, see the dict.update() method documentation:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

like image 92
Martijn Pieters Avatar answered Sep 21 '22 23:09

Martijn Pieters