Python Dictionary fromkeys() MethodPython Functions

Difficulty Level:
Difficulty Level:

The fromkeys() function is used to create a new dictionary, with the elements in the sequence seq as the keys and the value as the initial value corresponding to all keys.


# -*- coding: UTF-8 -*-

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

dict1 = dict.fromkeys(seq)
print("the dict1 is : ", dict1 )

dict2 = dict.fromkeys(seq, 2019)
print("the dict2 is: " ,dict2)


the dict1 is :  {'name': None, 'age': None, 'sex': None, 'address': None, 'ID': None}
the dict2 is:  {'name': 2019, 'age': 2019, 'sex': 2019, 'address': 2019, 'ID': 2019}


dictionary.fromkeys(seq[, value])


Name Description
seq The sequence of elements which is to be used as keys for the new dictionary.
value The value which is corresponding to all keys.

Return Value

It returns a new dictionary.


© 2021, all rights reserved. Privacy Policy | Contact Us