Python dictionary (Dictionary) fromkeys () method
description
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.
grammar
fromkeys () method syntax:
dict.fromkeys(seq[, value]))
parameter
- seq - the list of dictionary keys.
- value - optional parameter setting key sequence (seq) value.
return value
This method returns a list.
Examples
The following example shows fromkeys () function to use:
#!/usr/bin/python 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}