def filter(function, dictionary, recursive=True):
    """

    Return those items of the dictionary for which function(item) is true. The
    item is a tuple (key,value). If function is None, return the items with 
    keys that are true.
    """
    result = {}
    for key in dictionary:
        value = dictionary[key]
        if function(key, value):
            if recursive and type(value) == dict:
                value = filter(function, value)
            result[key] = value
    return result
