import collections


def _list_to_dicts(list_of_values, keys):
  """Restores a list of dicts from a list created by `_dicts_to_list`.

  `keys` must be the same as what was used in `_dicts_to_list` to create the
  list. This is used to restore the original dicts inside `host_call_fn` and
  `metrics_fn`.

  Transforms a list to a list of dicts.

  Args:
    list_of_values: (list) a list of values. The length must a multiple of the
      length of keys.
    keys: (list) a list of keys.

  Returns:
    (list) a list of dicts.
  """
  num_dicts = len(list_of_values) // len(keys)
  list_of_dicts = [collections.OrderedDict() for i in range(num_dicts)]
  for i, key in enumerate(keys):
    for j in range(num_dicts):
      list_of_dicts[j][key] = list_of_values[i * num_dicts + j]
  return list_of_dicts