util.dicts.filter
Similar to list filter
, this function takes a dictionary and filters out key-value pairs based on a filtering function that returns a bool
.
The function fn
should be in the form def fn(key=None, val=None)
. Both keyword arguments are mandatory.
Arguments:
def util.dicts.filter(
d: dict = None,
fn: function = None,
):
Returns a dictionary with values filtered out.
Example:
def key_fn(key='', val=''):
return key == 'bob' or val['name'] == 'Charles'
# {
# 'bob': {'name': 'Bob'},
# 'charles': {'name': 'Charles'},
# }
return util.dicts.filter(
d={
'john': {'name': 'John'},
'bob': {'name': 'Bob'},
'charles': {'name': 'Charles'},
},
fn=key_fn)