def difference_pyramid(pyr, inplace=False):
    """Difference between levels of a derivative of gaussian pyramid

    :param d: gaussian pyramid
    :param inplace: bool, do the work in place or make a copy, default False
    :return: list of differences

    The output has length one less than the input
    diff_pyr = [pyr[0] - pyr[1], pyr[1] - pyr[2], ..., pyr[N-2] - pyr[N-1]]
    """
    N = len(pyr)
    if type(pyr[0]) == list:
        M = len(pyr[0])
    else:
        M = 1
    if inplace:
        output = pyr
    elif M > 1:
        output = [[None] * M] * N
    else:
        output = [None] * N
    for n in range(N - 1):
        if M > 1:
            for m in range(M):
                output[n][m] = pyr[n][m] - pyr[n + 1][m]
        else:
            output[n] = pyr[n] - pyr[n + 1]
    output = output[:-1]
    return output
