code: def filter_values(d, vals=None, list_of_dicts=False, deepcopy=True):
      
      vals = [] if vals is None else vals
      list_of_dicts = '__list__' if list_of_dicts else None
  
      flatd = flatten(d, list_of_dicts=list_of_dicts)
  
      def is_in(a, b):
          try:
              return a in b
          except Exception:
              return False
  
      flatd = {k: v for k, v in flatd.items() if is_in(v, vals)}
      return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)
docs: filters leaf nodes of nested dictionary
  
      Parameters
      ----------
      d : dict
      vals : list
          values to filter by
      list_of_dicts: bool
          treat list of dicts as additional branches
      deepcopy: bool
          deepcopy values
  
      Examples
      --------
  
      >>> d = {1:{"a":"A"},2:{"b":"B"},4:{5:{6:'a'}}}
      >>> filter_values(d,['a'])
      {4: {5: {6: 'a'}}}