def swapdim(P, dim1=1, dim2=0):
    """
    Swap the dim between two variables.

    Args:
        P (Poly):
            Input polynomial.
        dim1 (int):
            First dim
        dim2 (int):
            Second dim.

    Returns:
        (Poly):
            Polynomial with swapped dimensions.

    Examples:
        >>> x,y = variable(2)
        >>> P = x**4-y
        >>> print(P)
        q0^4-q1
        >>> print(swapdim(P))
        q1^4-q0
    """
    if not isinstance(P, Poly):
        return numpy.swapaxes(P, dim1, dim2)

    dim = P.dim
    shape = P.shape
    dtype = P.dtype

    if dim1==dim2:
        return P

    m = max(dim1, dim2)
    if P.dim <= m:
        P = chaospy.poly.dimension.setdim(P, m+1)
        dim = m+1

    A = {}

    for key in P.keys:

        val = P.A[key]
        key = list(key)
        key[dim1], key[dim2] = key[dim2], key[dim1]
        A[tuple(key)] = val

    return Poly(A, dim, shape, dtype)