Python dictionary fromkeys() method

Python Dictionary fromkeys() Method

Description

The Python dictionary method fromkeys() creates a new dictionary using the keys from seq and sets the values to value.

Syntax

The syntax of the

fromkeys() method is as follows:

dict.fromkeys(seq[, value])

Parameters

  • seq − This is the list of values to be prepared for the dictionary keys.
  • value − This is optional. If provided, value is set to that value.

Return Value

This method returns a list.

Example

The following example shows the usage of the fromkeys() method.

dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" % len(dict)
dict.clear()
print "End Len : %d" % len(dict)

When we run the above program, the following output is produced:

When we run the above program, the following output is produced −

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

Leave a Reply

Your email address will not be published. Required fields are marked *