Non-standard dictionary functions
Function | dict |
Zip dictionaries. |
Function | filter |
Filter a dictionary by a predicate on key-value pairs. |
Function | filter |
Filter a dictionary by a predicate on keys. |
Function | filter |
Filter a dictionary by a predicate on values. |
Function | map |
Map a binary function over the key-value pairs in a dictionary. |
Function | map |
Map a function over the keys in a dictionary. |
Function | map |
Map a function over the values in a dictionary. |
Function | sub |
Create a dictionary from a subset of another. |
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)) {}
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
Filter a dictionary by a predicate on keys. >>> filter_keys(lambda n: n % 2, {0: 1, 1: 2, 2: 3}) == {1: 2} True
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
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
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
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