get_dict_values

pyhelpers.ops.get_dict_values(key, dictionary)

Get all values in a (nested) dictionary for a given key.

See also [OPS-GDV-1] and [OPS-GDV-2].

Parameters
  • key (any) – any that can be the key of a dictionary

  • dictionary (dict) – a (nested) dictionary

Returns

all values of the key within the given target_dict

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']]