def discard_n(seq, value, n=None):
    """
    Discard at most `n` occurrences of `value`. If `n` is not given, all
    occurrences will be removed.

    Examples:
        >>> L = [1, 0, 1, 0, 0, 1, 1, 0, 0]
        >>> discard_n(L, 0, 2); L
        [1, 1, 0, 1, 1, 0, 0]
        >>> discard_n(L, 1); L
        [0, 0, 0]
    """
    n = n if n is not None else len(seq)
    idx = 0
    for _ in range(len(seq)):
        if seq[idx] == value:
            if n > 0:
                n -= 1
                del seq[idx]
            else:
                break
        else:
            idx += 1
