def make_list_of_pairs(l):
    """
    Converts input from several possible formats into a list of pairs: used
    to clean input for swap-like moves.

    Allowed input formats:
    * flat list of length 2N
    * list of pairs
    * None (returns None)

    Anything else will lead to a ValueError or AssertionError

    Parameters
    ----------
    l : list
        input list, either flat list of length 2N, a list of pairs or None

    Returns
    -------
    list of pairs
    """
    if l is None:
        return None

    _ = len(l)  # raises TypeError, avoids everything else

    # based on first element, decide whether this should be a list of lists
    # or a flat list
    try:
        _ = len(l[0])
        list_of_lists = True
    except TypeError:
        list_of_lists = False

    if list_of_lists:
        for elem in l:
            assert len(elem) == 2, "List of lists: inner list length != 2"
        outlist = l
    else:
        assert len(l) % 2 == 0, "Flattened list: length not divisible by 2"
        outlist = [
            [a, b] for (a, b) in zip(l[slice(0, None, 2)], l[slice(1, None, 2)])
        ]
    # Note that one thing we don't check is whether the items are of the
    # same type. That might be worth doing someday; for now, we trust that
    # part to work.
    return outlist