Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python dictionary setdefault() Method


previous next AddThis Social Bookmark Button


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:

  • key: This is the key be searched.

  • defualt: Value to be returned in case key is not found.

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 Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names