setdefault(): set a default value and then return it. : setdefault « Dictionary « Python Tutorial






myDict = {'host': 'earth', 'port': 80}
print myDict.keys()
print myDict.items()
print myDict.setdefault('port', 8080)
print myDict.setdefault('prot', 'tcp')
print myDict.items()








8.23.setdefault
8.23.1.setdefault sets the value corresponding to the given key if it is not already in the dictionary
8.23.2.setdefault(): set a default value and then return it.