recursive_get

Contents

recursive_get#

recursive_get(dict_, key, default=None)[source]#

Recursively get a key from a nested dictionary.

The key is first looked for at the top level of dict_, then in its nested dictionaries (depth first, in insertion order). The first non-None value found is returned.

Parameters:
  • dict (dict) – The dictionary to search.

  • key (str) – The key to search for.

  • default (object, optional) – The value to return if the key is not found. Default is None.

Returns:

object – The value of the key if found, otherwise default.

Examples

>>> recursive_get({"a": 1, "b": {"c": 2}}, "c")
2