skbase.utils.subset_dict_keys#
- skbase.utils.subset_dict_keys(input_dict: MutableMapping[Any, Any], keys: Iterable | int | float | bool | str | type, prefix: str | None = None, remove_prefix: bool = True)[source]#
Subset dictionary so it only contains specified keys.
Subsets input_dict so that it only contains keys. If prefix is passed, subsets to f”{prefix}__{key}” for all key in keys. When
remove_prefix=Truethe the prefix is removed from the keys of the return dictionary (For any keys with prefix the return is {key} instead of f”{prefix}__{key}”).- Parameters:
- input_dictdict
Dictionary to subset by keys
- keysiterable, int, float, bool, str or type
The keys that should be retained in the output dictionary.
- prefixstr, default=None
An optional prefix that is added to all keys. If prefix is passed, the passed keys are converted to f”{prefix}__{key}” when subsetting the dictionary. Results in all keys being coerced to str.
- remove_prefixbool, default=True
Whether to remove prefix in output keys.
- Returns:
- `subsetted_dict`dict
dict_to_subset subset to keys in keys described as above
Notes
Passing prefix will turn non-str keys into str keys.
Examples
>>> from skbase.utils import subset_dict_keys >>> some_dict = {"some_param__a": 1, "some_param__b": 2, "some_param__c": 3}
>>> subset_dict_keys(some_dict, "some_param__a") {'some_param__a': 1}
>>> subset_dict_keys(some_dict, ("some_param__a", "some_param__b")) {'some_param__a': 1, 'some_param__b': 2}
>>> subset_dict_keys(some_dict, ("a", "b"), prefix="some_param") {'a': 1, 'b': 2}
>>> subset_dict_keys(some_dict, ("a", "b"), prefix="some_param", remove_prefix=False) {'some_param__a': 1, 'some_param__b': 2}
>>> subset_dict_keys(some_dict, (c for c in ("some_param__a", "some_param__b"))) {'some_param__a': 1, 'some_param__b': 2}