def grad_list(val, tensors, all_in_one = True):
    """
    Compute the gradient w.r.t. the cores of several given TT-tensors (or TT-oeprators).
    Watch must be called on all of them beforehand.
    
    Args:
        val (torch.tensor): scalar tensor to be differentiated.
        tensors (list[torch.TT]): the tensors with respect to which the differentiation is made.
        all_in_one (bool, optional): Put all the cores in one list or create a list of lists with the cores. Defaults to True.
    
    Returns:
        list[list[torchtt.TT]]: the resulting derivatives.
    """
    val.backward()
    cores_list = []
    if all_in_one:
        for t in tensors:
            cores_list += [ c.grad for c in t.cores]
    else:
        for t in tensors:
            cores_list.append([ c.grad for c in t.cores])
    return cores_list