Python‎ > ‎dict‎ > ‎

dict.fromkeys()


Introduction
The method returns with the new dictionary with the values remains same.

Syntax
dict.fromkeys(x)

Parameters
x=dictionary key

Examples
The method return the corresponding value of the key.
>>> x=a=dict({'Din': 4, 'John': 2, 'Adil': 3, 'Fen': 1})
>>> a=dict({'Din': 4, 'John': 2, 'Adil': 3, 'Fen': 1})
>>> a.fromkeys('Din')
{'i': None, 'n': None, 'D': None}
>>> a.fromkeys('John')
{'n': None, 'J': None, 'o': None, 'h': None}
>>> a.fromkeys('Adil')
{'d': None, 'A': None, 'i': None, 'l': None}
>>>

The method returns with:
 >>> x=dict({'VA':3.1, 'DC':2,'NY':5.1, 'CA':4.4})
>>> x.fromkeys('VA')
{'A': None, 'V': None}
>>> x.fromkeys('DC')
{'C': None, 'D': None}
>>> x.fromkeys('NY')
{'N': None, 'Y': None}
>>>

Comments