Copyright © tutorialspoint.com

Python dictionary setdefault() Method

previous next


Description:

This class method is similar to get(), but will set dict[key]=default if key is not already in dict.

Syntax:

dict.setdefault(key, default=None)

Parameters:

Here is the detail of parameters:

Example:

#!/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

previous next

Copyright © tutorialspoint.com