import re


def unflat(data, separator="_", lseparator=('[', ']')):
    """
    Unflat the dict

    Args:
        data (dict): the dict to unflat. Must be a key/value dict.
        separator (:obj:`str`, optional): key separator.

    Returns:
        dict: Unflatted dict
    """
    unflat_dict = {}
    for k in sorted(data.keys()):
        context = unflat_dict
        for sub_key in k.split(separator)[:-1]:
            if sub_key not in context:
                context[sub_key] = {}
            context = context[sub_key]

        key = k.split(separator)[-1]

        regex = r'(\w*)\{0}(\d)\{1}'.format(lseparator[0], lseparator[1])
        match = re.match(regex, key)
        if match:
            lkey = match.group(1)
            lpos = int(match.group(2))

            if not lkey in context:
                context[lkey] = []
            context[lkey].insert(lpos, data[k])
        else:
            context[key] = data[k]
    return unflat_dict