dict.fromkeys
behaviourIf 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,
}
"""
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”.
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']
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.