def collapse(arr, axis=-1):
    """Collapse singleton dimension (first or last) in an array.

    Parameters
    ----------
    arr : ndarray
        Array to collapse.
    axis : {0, -1}
        Axis to collapse.

    Returns
    -------
    output : ndarray
        Array with singleton dimension at beginning or end removed.
    """

    if axis not in [0, -1]:
        raise ValueError('Invalid axis %d.  Must be 0 or -1.' % axis)

    dims = arr.shape
    if dims[axis] > 1:
        raise ValueError('Dimension %d of input array is not singleton.' % axis)
    if axis == 0:
        output = arr[0]
    else:
        output = arr[...,0]

    return output