get_dict_values¶
- pyhelpers.ops.get_dict_values(key, dictionary)[source]¶
Retrieves all values in a (nested) dictionary for a given key.
See also [OPS-GDV-1] and [OPS-GDV-2].
- Parameters:
key (any) – The key to search for in the dictionary.
dictionary (dict) – The (nested) dictionary to search within.
- Returns:
A generator yielding all values associated with the given
key
within thedictionary
.- Return type:
Generator[Iterable]
Examples:
>>> from pyhelpers.ops import get_dict_values >>> key_ = 'key' >>> target_dict_ = {'key': 'val'} >>> val = get_dict_values(key_, target_dict_) >>> list(val) [['val']] >>> key_ = 'k1' >>> target_dict_ = {'key': {'k1': 'v1', 'k2': 'v2'}} >>> val = get_dict_values(key_, target_dict_) >>> list(val) [['v1']] >>> key_ = 'k1' >>> target_dict_ = {'key': {'k1': ['v1', 'v1_1']}} >>> val = get_dict_values(key_, target_dict_) >>> list(val) [['v1', 'v1_1']] >>> key_ = 'k2' >>> target_dict_ = {'key': {'k1': 'v1', 'k2': ['v2', 'v2_1']}} >>> val = get_dict_values(key_, target_dict_) >>> list(val) [['v2', 'v2_1']]