setdefault sets the value corresponding to the given key if it is not already in the dictionary : Setdefault : Dictionary PYTHON TUTORIALS


PYTHON TUTORIALS » Dictionary » Setdefault »

 

setdefault sets the value corresponding to the given key if it is not already in the dictionary


# when the key is missing, setdefault returns the default and updates the dictionary accordingly. 


d = {}
d.setdefault('name', 'N/A')
d['name'] 'Gumby'
d.setdefault('name', 'N/A')
print d



    Related Scripts with Example Source Code in same category :

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Dictionary
» Setdefault