def _to_list(x, n):
    """Converts x into list by repeating it n times.
    If x is already a list and it has length n, returns x.
    Else, if x is a list and has different length, raises ValueError."""
    if isinstance(x, list):
        if len(x) != n:
            raise ValueError(
                '''If list is passed, it should have len(x) == n.
                Found: n = %d, len(x) = %d''' % (n, len(x))
            )
        return x
    return [x] * n