def _pad_jagged_sequence(seq):
    """
    Pads a 2D jagged sequence with the default value of the element type to make it rectangular.
    The type of each sequence (tuple, list, etc) is maintained.
    """

    columns = max(map(len, seq))  # gets length of the longest row
    return type(seq)(
        (
            sub_seq + type(sub_seq)(type(sub_seq[0])() for _ in range(columns - len(sub_seq)))
            for sub_seq in seq
        )
    )