Default dict.fromkeys behaviour

If you have an iterable of keys, you can initialise a dictionary with them by using the class method dict.fromkeys, which creates a dictionary where all values are set to None:

keys = ["name", "age", "address"]
person_info = dict.fromkeys(keys)

print(person_info)
"""
{
    'name': None,
    'age': None,
    'address': None,
}
"""

Initiliasing with a custom value

However, apparently you can also set a custom value if you pass in a second argument to dict.fromkeys:

keys = ["name", "age", "address"]
person_info = dict.fromkeys(keys, "")

print(person_info)
"""
{
    'name': "",
    'age': "",
    'address': "",
}
"""

This is an “obvious” argument to include, but I didn't know this was possible... I wonder if it was there all along and I didn't know, or if it was added “recently”.

Mutable value as the initial value

If you use a mutable value as the initial value, that mutable value will be shared across keys and you might end up with some funky situations:

keys = ["a", "b", "c"]
my_dict = dict.fromkeys(keys, [])

my_dict["a"].append("Hello")
my_dict["b"].append("there")
print(my_dict["c"])  # ['Hello', 'there']

Become a better Python 🐍 developer, drop by drop 💧

Get a daily drop of Python knowledge. A short, effective tip to start writing better Python code: more idiomatic, more effective, more efficient, with fewer bugs. Subscribe here.

Previous Post

Blog Comments powered by Disqus.