Latest web development tutorials

Python dictionary (Dictionary) fromkeys () method


Python dictionary (Dictionary) fromkeys () function is used to create a new dictionary to the sequence seq elements do dictionary key, value as a dictionary of all the key corresponding to the initial value.


fromkeys () method syntax:

dict.fromkeys(seq[, value]))


  • seq - the list of dictionary keys.
  • value - optional parameter setting key sequence (seq) value.

return value

This method returns a list.


The following example shows fromkeys () function to use:


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

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

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

The above example output is:

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