def keyfilter(predicate, d, factory=dict):
 rv = factory()
 for (k, v) in iteritems(d):
  if predicate(k):
   rv[k] = v
 return rv

def keyfilter ( predicate , d , factory = dict ) : rv = factory ( ) for ( k , v ) in iteritems ( d ) : if predicate ( k ) : rv [ k ] = v return rv
filter items in dictionary by key .