Posts

Showing posts with the label fromkeys

How to initialize a dictionary from a list of keys?

How to initialize a dictionary from a list of keys using the classmethod fromkeys

The standard dict class has a classmethod .fromkeys which accepts a list of keys and optionally a value. In our example we have a list of feature names that are all from type float. Using fromkeys we can initialize a dictionary with the feature names as keys and value float. If no value is provided in fromkeys, it defaults to None. 


Github gist with code

dependencies: python3.9