from typing import Sequence
from typing import List
import copy


def cut_list_to_limit_concatenated_str_length(
    target_list: Sequence[str], separator: str,
    max_total_str_length: int) -> List[str]:
  """Removes the last items from the list to limit the length of concatenated string of the items.

  For example, when target_list = ['Hello', 'Shoptimizer'] and separator = ',',
  the concatenated string is 'Hello,Shoptimizer' and the length of the string is
  17. If max_total_str_length = 10, the length exceeds the maximum. So, this
  function removes the last item and returns transformed list: ['Hello'].

  Args:
    target_list: A list to be cut.
    separator: Characters used as separator when concatenating the items in the
      list.
    max_total_str_length: The maximum length of the concatenated string.

  Returns:
    A list cut from target_list.
  """
  output_list = list(copy.deepcopy(target_list))
  # Concatenated string length > max str length..
  while len(separator.join(output_list)) > max_total_str_length:
    output_list.pop()
  return output_list