def reDict(aDict, func, cond=(lambda k,v:1), delUnchanged=0):
    '''
    Given aDict, return a new dict with valid items (= items whose
    cond(key, value) is true) been modified by the function func.
    if delUnchanged, then those invalid items are excluded from
    the returned dict.
    
    >>> d
    {'B': 1, 'D': 3}
    >>> reDict(d, lambda x:x**2)
    {'B': 1, 'D': 9}
    >>> g
    {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0)
    {'A': 0, 'C': -2, 'B': 1, 'E': -4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0, delUnchanged=1)
    {'A': 0, 'C': -2, 'E': -4}    

    '''
    tmp={}
    if delUnchanged:
       [tmp.setdefault(k,func(v)) for k,v in aDict.items() if cond(k,v)]
    else:
       [tmp.setdefault(k,(cond(k,v) and func(v) or v)) for k,v in aDict.items()]
    return tmp.copy()

    Given aDict, return a new dict with valid items (= items whose
    cond(key, value) is true) been modified by the function func.
    if delUnchanged, then those invalid items are excluded from
    the returned dict.
    
    >>> d
    {'B': 1, 'D': 3}
    >>> reDict(d, lambda x:x**2)
    {'B': 1, 'D': 9}
    >>> g
    {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0)
    {'A': 0, 'C': -2, 'B': 1, 'E': -4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0, delUnchanged=1)
    {'A': 0, 'C': -2, 'E': -4}    

    
recipes/Python/278259_Dictionary_Tools/recipe-278259.py