This class method is similar to get(), but will set dict[key]=default if key is not already in dict.
dict.setdefault(key, default=None)
Here is the detail of parameters:
key: This is the key be searched.
defualt: Value to be returned in case key is not found.
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.setdefault('Age', None) print "Value : %s" % dict.setdefault('Sex', None)
This produces following result:
Value : 7 Value : None
Advertisement