def distribute(lis, columns, direction, fill=None):
    """Distribute a list into a N-column table (list of lists).

    ``lis`` is a list of values to distribute.

    ``columns`` is an int greater than 1, specifying the number of columns in
    the table.

    ``direction`` is a string beginning with "H" (horizontal) or "V"
    (vertical), case insensitive.  This affects how values are distributed in
    the table, as described below.

    ``fill`` is a value that will be placed in any remaining cells if the data
    runs out before the last row or column is completed.  This must be an 
    immutable value such as ``None`` , ``""``, 0, "&nbsp;", etc.  If you
    use a mutable value like ``[]`` and later change any cell containing the
    fill value, all other cells containing the fill value will also be changed.

    The return value is a list of lists, where each sublist represents a row in
    the table.
    ``table[0]`` is the first row.
    ``table[0][0]`` is the first column in the first row.
    ``table[0][1]`` is the second column in the first row.

    This can be displayed in an HTML table via the following Mako template:

    .. code-block:: html+mako

        <table>
        % for row in table:
          <tr>
        % for cell in row:
            <td>${cell}</td>
        % endfor   cell
          </tr>
        % endfor   row
        </table>

    In a horizontal table, each row is filled before going on to the next row.
    This is the same as dividing the list into chunks::

        >>> distribute([1, 2, 3, 4, 5, 6, 7, 8], 3, "H")
        [[1, 2, 3], [4, 5, 6], [7, 8, None]]

    In a vertical table, the first element of each sublist is filled before
    going on to the second element.  This is useful for displaying an
    alphabetical list in columns, or when the entire column will be placed in
    a single <td> with a <br /> between each element::

        >>> food = ["apple", "banana", "carrot", "daikon", "egg", "fish", "gelato", "honey"]
        >>> table = distribute(food, 3, "V", "")
        >>> table
        [['apple', 'daikon', 'gelato'], ['banana', 'egg', 'honey'], ['carrot', 'fish', '']]
        >>> for row in table:
        ...    for item in row:
        ...         print "%-9s" % item,
        ...    print "."   # To show where the line ends.
        ...
        apple     daikon    gelato    .
        banana    egg       honey     .
        carrot    fish                .

    Alternatives to this function include a NumPy matrix of objects.

    """
    if columns < 1:
        raise ValueError("arg 'columns' must be >= 1")
    dir = direction[0].upper()
    if dir == 'H':
        table = []
        for i in range(0, len(lis), columns):
            row = lis[i:i + columns]
            row_len = len(row)
            if row_len < columns:
                extra = [fill] * (columns - row_len)
                row.extend(extra)
            table.append(row)
        return table
    elif dir == 'V':
        total = len(lis)
        rows, remainder = divmod(total, columns)
        if remainder:
            rows += 1
        table = [([fill] * columns) for x in range(rows)]
        for i, elm in enumerate(lis):
            col, row = divmod(i, rows)
            table[row][col] = elm
        return table
    else:
        raise ValueError("arg ``direction`` must start with 'H' or 'V'")
