import bisect


def bottom_values(values, period=None, num=1):
    """Returns list of bottom num items.

    :param values: list of values to iterate and compute stat.
    :param period: (optional) # of values included in computation.
        * None - includes all values in computation.
    :param num: the num in the bottom num items.
    :rtype: list of windowed bottom num items.

    Examples:
    >>> values = [34, 30, 29, 34, 38, 25, 35]
    >>> bottom_values(values, 3, 2)  #3 period window and top 2 items.
    [[34], [30, 34], [29, 30], [29, 30], [29, 34], [25, 34], [25, 35]]
    """
    if period:
        if period < 1:
            raise ValueError("period must be 1 or greater")

        period = int(period)

    if num:
        num = int(num)

    results = []
    recs = []
    _additem = bisect.insort
    _search = bisect.bisect_left

    for bar, newx in enumerate(values):
        if period and (bar >= period):
            item = values[bar - period]
            idx = _search(recs, item)
            del recs[idx]

        _additem(recs, newx)

        endidx = num
        if bar < num - 1:
            endidx = bar + 1

        lastval = recs[0:endidx]

        results.append(lastval)

    return results