import math, random


def split_list(data, splits={'train': 8, 'test': 2}, shuffle=True, seed=0):
    """
    Split the list according to a given ratio

    Args:
        data (list): a list of data to split
        splits (dict): a dictionary specifying the ratio of splits
        shuffle (bool): shuffle the list before
        seed (int): random seed used for shuffling

    Returns:
        a dictionary of the splitted list
    """
    data = data.copy() # work on a copy of the oridinal list
    n_tot = len(data)
    split_tot = float(sum([v for v in splits.values()]))
    n_split = {k:math.ceil(n_tot*v/split_tot) for k,v in splits.items()}
    if shuffle:
        random.seed(seed)
        random.shuffle(data)
    splitted = {}
    cnt = 0
    for k, v in n_split.items():
        splitted[k] = data[cnt:cnt+v]
        cnt += v
    return splitted