import six


def get_formula_from_symbol_list(_list, separator=""):
    """
    Return a string with the formula obtained from the list of symbols.
    Examples:
    * ``[[1,'Ba'],[1,'Ti'],[3,'O']]`` will return ``'BaTiO3'``
    * ``[[2, [ [1, 'Ba'], [1, 'Ti'] ] ]]`` will return ``'(BaTi)2'``

    :param _list: a list of symbols and multiplicities as obtained from
        the function group_symbols
    :param separator: a string used to concatenate symbols. Default empty.

    :return: a string
    """

    list_str = []
    for elem in _list:
        if elem[0] == 1:
            multiplicity_str = ''
        else:
            multiplicity_str = str(elem[0])

        if isinstance(elem[1], six.string_types):
            list_str.append("{}{}".format(elem[1], multiplicity_str))
        elif elem[0] > 1:
            list_str.append(
                "({}){}".format(get_formula_from_symbol_list(elem[1],
                                                             separator=separator),
                                multiplicity_str))
        else:
            list_str.append("{}{}".format(get_formula_from_symbol_list(elem[1],
                                                                       separator=separator),
                                          multiplicity_str))

    return separator.join(list_str)