Latest web development tutorials

Python3 dictionary fromkeys () method

Python3 dictionary Python3 dictionary


description

Python 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/python3

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

dict = dict.fromkeys(seq)
print ("新的字典为 : %s" %  str(dict))

dict = dict.fromkeys(seq, 10)
print ("新的字典为 : %s" %  str(dict))

The above example output is:

新的字典为 : {'age': None, 'name': None, 'sex': None}
新的字典为 : {'age': 10, 'name': 10, 'sex': 10}

Python3 dictionary Python3 dictionary