Change value for a key in Python dictionary

Update value key pair

The update method changes the value for a given key. update method has the following syntax.

dict1.update(dict2)

Merges all of dict2's entries into dict1, in-place, similar to for (k, v) in dict2.items(): dict1[k] = v.


d = { #   ww w  .j  a v a  2 s.c o m
       'title': 'Python Web Site', 
       'url': 'http://www.java2s.com', 
       'changed': 'Mar 14 22:09:15 MET 2005' 
} 
x = {'title': 'Python Language Website'} 
print d.update(x) 
print d 

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary