human
Complete this python program:
def itemfilter(predicate, d, factory=dict):
assistant
#Here is the Python code you requested.

    """Filter items in dictionary by item
    >>> def isvalid(item):
    ...     k, v = item
    ...     return k % 2 == 0 and v < 4
    >>> d = {1: 2, 2: 3, 3: 4, 4: 5}
    >>> itemfilter(isvalid, d)
    {2: 3}
    See Also:
    keyfilter
    valfilter
    itemmap
    """
    rv = factory()
    for item in iteritems(d):
        if predicate(item):
            (k, v) = item
            rv[k] = v
    return rv