Copyright © tutorialspoint.com

Python dictionary fromkeys() method

previous next


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:

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

Copyright © tutorialspoint.com