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 fromkeys() method


previous next AddThis Social Bookmark Button


Description:

This method creates a new dictionary with keys from seq and values set to value.

Syntax:

dict.fromkeys(seq[, value]))

Parameters:

Here is the detail of parameters:

  • seq: This is the list of values which would be used for dictionary keys preparation.

  • value: This is option, if provided then value would be set to this value.

Example:

#!/usr/bin/python

seq = ('name', 'age', 'sex')

dict = dict.fromkeys(seq)
print "New Dictinary : %s" %  str(dict)

dict = dict.fromkeys(seq, 10)
print "New Dictinary : %s" %  str(dict)

This produces following result:

New Dictinary : {'age': None, 'name': None, 'sex': None}
New Dictinary : {'age': 10, 'name': 10, 'sex': 10}


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names