import copy


def add_ancestors(terms, ancestors):
  """
  expand a list of terms to include their ancestors

  Args:
    terms: list of term indices
    ancestors: a dictionary of term indices (keys) with their ancestor terms indices (values)

  Returns:
    list of term indices
  """

  terms_expanded = copy.copy(terms)
  for term in terms:
    try:
      terms_expanded += ancestors[term]
    except KeyError:
      pass
  return list(set(terms_expanded))