def _unflatten_dict(flat_dict, prefixes):
  """Returns a dict of dicts if any prefixes match keys in the flat dict.

    The function handles the case where the prefix may not be a dict.

  Args:
    flat_dict: A dict without any nesting.
    prefixes: A list of strings which may have been dicts in the
      original structure.

  """
  original_dict = {}
  for key, value in flat_dict.items():
    prefix_found = False
    for prefix in prefixes:
      full_prefix = "__" + prefix + "_"
      if key.startswith(full_prefix):
        # Add a dict to the original dict with key=prefix
        if prefix not in original_dict:
          original_dict[prefix] = {}
        original_dict[prefix][key[len(full_prefix):]] = value
        prefix_found = True
        break
    if not prefix_found:
      # No key matched a prefix in the for loop.
      original_dict[key] = value

  return original_dict
def _unflatten_dict ( flat_dict , prefixes ) : original_dict = { } for key , value in flat_dict . items ( ) : prefix_found = False for prefix in prefixes : full_prefix = "__" + prefix + "_" if key . startswith ( full_prefix ) : if prefix not in original_dict : original_dict [ prefix ] = { } original_dict [ prefix ] [ key [ len ( full_prefix ) : ] ] = value prefix_found = True break if not prefix_found : original_dict [ key ] = value return original_dict
Returns a dict of dicts if any prefixes match keys in the flat dict.

    The function handles the case where the prefix may not be a dict.

  Args:
    flat_dict: A dict without any nesting.
    prefixes: A list of strings which may have been dicts in the
      original structure.