module documentation

Non-standard dictionary functions

Function dict_zip Zip dictionaries.
Function filter_items Filter a dictionary by a predicate on key-value pairs.
Function filter_keys Filter a dictionary by a predicate on keys.
Function filter_values Filter a dictionary by a predicate on values.
Function map_items Map a binary function over the key-value pairs in a dictionary.
Function map_keys Map a function over the keys in a dictionary.
Function map_values Map a function over the values in a dictionary.
Function sub_dict Create a dictionary from a subset of another.
def dict_zip(*dicts): (source)

Zip dictionaries. >>> dict_zip(dict(a=True), dict(a='foo')) {'a': (True, 'foo')} >>> dict_zip(dict(a=0, b=2), dict(a=1, c=3), dict(a=None, c=4)) {'a': (0, 1, None)} >>> dict_zip(dict(a=0), dict(b=1, c=2, d=3)) {}

def filter_items(p, d): (source)

Filter a dictionary by a predicate on key-value pairs. >>> import operator >>> filter_items(operator.le, {0: 0, 1: 2, 3: 2}) == {0: 0, 1: 2} True

def filter_keys(p, d): (source)

Filter a dictionary by a predicate on keys. >>> filter_keys(lambda n: n % 2, {0: 1, 1: 2, 2: 3}) == {1: 2} True

def filter_values(p, d): (source)

Filter a dictionary by a predicate on values. >>> filter_values(lambda n: n % 2, {0: 1, 1: 2, 2: 3}) == {0: 1, 2: 3} True

def map_items(f, d): (source)

Map a binary function over the key-value pairs in a dictionary. >>> map_items(lambda a,b: (a*2, b**2), {1: 2, 3: 4}) == {2: 4, 6: 16} True

def map_keys(f, d): (source)

Map a function over the keys in a dictionary. >>> map_keys(str, {1: 2, 3: 4}) == {'1': 2, '3': 4} True >>> map_keys(lambda x: 0, {1: 2, 3: 4}) in [{0: 2}, {0: 4}] True

def map_values(f, d): (source)

Map a function over the values in a dictionary. >>> map_values(str, {1: 2, 3: 4}) == {1: '2', 3: '4'} True >>> map_values(lambda x: 0, {1: 2, 3: 4}) == {1: 0, 3: 0} True

def sub_dict(d, keys): (source)

Create a dictionary from a subset of another. >>> sub_dict({1: 2, 3: 4, 'a': 'b'}, [1, 3]) == {1: 2, 3: 4} True >>> try: ... sub_dict({1: 2}, [1, 3]) ... assert False ... except KeyError: ... print('Key error!') Key error!