Home / Python / Reference / Dictionary methods

Python dictionary fromkeys() method

Make a new dictionary with given keys, all set to one value.

Syntax

dict.fromkeys(keys, value=None)

Use an immutable value (a number, None, a string). With a list as the value, every key would share one list.

Parameters

keysthe keys
valuethe value for every key

Return value

dict

Example

Code

print(dict.fromkeys(["a", "b", "c"], 0))
print(list(dict.fromkeys(["b", "a", "b", "c", "a"])))   # remove duplicates, keep order

Output

{'a': 0, 'b': 0, 'c': 0}
['b', 'a', 'c']

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.