Python dictionary (Dictionary) setdefault () method
description
Python dictionary (Dictionary) setdefault () function and get () method is similar, if the key does not already exist in the dictionary, it will be add keys and values to the default values.
grammar
setdefault () method syntax:
dict.setdefault(key, default=None)
parameter
- key - to find the key.
- default - default key when the key does not exist setup.
return value
This method has no return value.
Examples
The following example shows setdefault () function to use:
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.setdefault('Age', None) print "Value : %s" % dict.setdefault('Sex', None)
The above example output is:
Value : 7 Value : None