#NEWLINE# def keyfilter( predicate , d , factory = dict ) : #NEWLINE# #INDENT# ' Filter items in dictionary by key\n\n    >>> iseven = lambda x: x % 2 == 0\n    >>> d = {1: 2, 2: 3, 3: 4, 4: 5}\n    >>> keyfilter(iseven, d)\n    {2: 3, 4: 5}\n\n    See Also:\n        valfilter\n        itemfilter\n        keymap\n    ' #NEWLINE# rv = factory ( ) #NEWLINE# for ( k , v ) in iteritems ( d ) : #NEWLINE# #INDENT# if predicate ( k ) : #NEWLINE# #INDENT# rv [ k ] = predicate #NEWLINE# #UNINDENT# #UNINDENT# return rv