collectiontools#

This package implements functions to handle collections efficiently.

collectiontools.append_values(x: Dict[Any, List], y: Mapping) Dict[Any, List]#

Append values to a dictionary of lists.

Parameters:
  • x – Dictionary to append to.

  • y – Values to append.

Returns:

Input dictionary x with values of y appended.

Examples

>>> from collectiontools import append_values
>>>
>>> x = {}
>>> append_values(x, {"a": 1, "b": "c"})
{'a': [1], 'b': ['c']}
>>> append_values(x, {"a": 2, "b": "d"})
{'a': [1, 2], 'b': ['c', 'd']}
collectiontools.filter_values(predicate: Callable, x: dict) dict#

Filter a dictionary by values like filter() for iterables.

Parameters:
  • predicate – Predicate to evaluate on values of x. Values are included if predicate evaluates to True.

  • x – Dictionary to filter.

Returns:

Filtered dictionary.

Examples

>>> from collectiontools import filter_values
>>>
>>> filter_values(lambda x: isinstance(x, int), {"a": 1, "b": 2, "c": "hello"})
{'a': 1, 'b': 2}
collectiontools.map_values(func: Callable, x: dict, recursive: bool = False) dict#

Map a function over values of a dictionary like map() for iterables.

Parameters:
  • func – Function to apply to values of x.

  • x – Dictionary whose values to map func over.

  • recursive – Apply func recursively to nested dictionaries.

Returns:

Dictionary with values obtained by applying func to the values of x.

Examples

>>> from collectiontools import map_values
>>>
>>> map_values(lambda x: 2 * x, {"a": 1, "b": "hello"})
{'a': 2, 'b': 'hellohello'}
>>>
>>> map_values(lambda x: 2 * x, {"a": {"b": 3}}, recursive=True)
{'a': {'b': 6}}
collectiontools.transpose(x: Mapping | Iterable) dict | list#

Transpose between iterables of mappings and mappings of iterables.

Parameters:

x – Iterable of mappings or mapping of iterables to transpose.

Returns:

Transposed mapping of iterables or iterable of mappings.

See also

Transposition is implemented by transpose_to_dict() and transpose_to_list().

Examples

>>> from collectiontools import transpose
>>>
>>> x = [{"a": 1, "b": "hello"}, {"a": 2, "b": "hello"}]
>>> y = transpose(x)
>>> y
{'a': [1, 2], 'b': ['hello', 'hello']}
>>> transpose(y) == x
True
collectiontools.transpose_to_dict(x: Iterable[Mapping]) Dict[Any, List]#

Transpose an iterable of mappings to a dictionary of lists.

Parameters:

x – Iterable to transpose.

Returns:

Dictionary of lists.

Examples

>>> from collectiontools import transpose_to_dict
>>>
>>> transpose_to_dict([{"a": 1, "b": "hello"}, {"a": 2, "b": "hello"}])
{'a': [1, 2], 'b': ['hello', 'hello']}
collectiontools.transpose_to_list(x: Mapping[Any, Sequence]) List[Dict]#

Transpose a mapping of iterables to a list of dictionaries.

collectiontools.union(x, y: Mapping | None = None, **kwargs)#

Union of two dictionaries, yielding a new instance.

Parameters:
  • x – Dictionary to update.

  • y – Values to update as a mapping (may contain Delete to delete a value).

  • **kwargs – Values to update as keyword arguments (may contain Delete to delete a value).

Returns:

Union of the dictionaries.

Examples

>>> from collectiontools import Delete, union
>>>
>>> x = {"a": 3, "b": 9}
>>> union(x, {"c": "hello"}, a=Delete)
{'b': 9, 'c': 'hello'}
>>> "a" in x
True
collectiontools.update(x: dict, y: Mapping | None = None, **kwargs)#

Update a dictionary in-place.

Parameters:
  • x – Dictionary to update.

  • y – Values to update as a mapping (may contain Delete to delete a value).

  • **kwargs – Values to update as keyword arguments (may contain Delete to delete a value).

Returns:

Updated dictionary.

Examples

>>> from collectiontools import Delete, update
>>>
>>> x = {"a": 3, "b": 9}
>>> update(x, {"c": "hello"}, a=Delete)
{'b': 9, 'c': 'hello'}
>>> "a" in x
False
class collectiontools.Delete#

Delete an object.

See also

union() and update() delete keys from a dictionary if the corresponding value is Delete.

collectiontools.dict_product(**iterables: Sequence) Iterable[dict]#

Cartesian product of named input iterables.

Parameters:

iterables – Named iterables.

Yields:

Dictionaries representing elements of the Cartesian product.

Examples

>>> from collectiontools import dict_product
>>>
>>> list(dict_product(a=range(2), b="xy"))
[{'a': 0, 'b': 'x'}, {'a': 0, 'b': 'y'}, {'a': 1, 'b': 'x'}, {'a': 1, 'b': 'y'}]