Home >>Python Dictionary Methods >Python Dictionary fromkeys() Method

Python Dictionary fromkeys() Method

Python Dictionary fromkeys() Method

Python Dictionary fromkeys() Method in python dictionary generate a new dictionary with the specified keys and the specified value.

Syntax:
dict.fromkeys(keys, value)

Parameter Values

Parameter Description
keys It is Required. An iterable specifying the keys to be transformed into the new dictionary
value It is Optional. The value need to be assigned for all keys
Here is an Example of Python Dictionary fromkeys() Method:

a = ('num1', 'num2', 'num3')
b = 5
newdict = dict.fromkeys(a, b)
print(newdict)

Output:
{'num1': 5, 'num2': 5, 'num3': 5}
Example 2:

ax = ('AB', 'BC', 'CD')
bx = 15
newdict1 = dict.fromkeys(ax, bx)
print(newdict1)

Output:
{'AB': 15, 'BC': 15, 'CD': 15}

No Sidebar ads