Chapters
Python114 chapters

ReferenceDictionary methods

dict.fromkeys()

Builds a dictionary from keys, all sharing one value.

Signature

dict.fromkeys(keys, value=None)

Parameters

NameMeans
keysan iterable of keys
valuethe value each gets

Returns

A new dict.

Example

Python
print(dict.fromkeys("ab", 0))
print(list(dict.fromkeys(["a", "b", "a"])))

Output

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

See also