brainstate.util.pop

Contents

brainstate.util.pop#

brainstate.util.pop(x, key)[source]#
Overloads:
  • x (FrozenDict[K, V]), key (K) → tuple[FrozenDict[K, V], V]

  • x (dict[K, V]), key (K) → tuple[dict[K, V], V]

Remove and return an item from a dictionary.

Works with both FrozenDict and regular dict, returning a new dictionary without the specified key along with the popped value.

Parameters:
Returns:

A tuple of (new dictionary without the key, popped value).

Return type:

tuple

Raises:
  • TypeError – If x is not a FrozenDict or dict.

  • KeyError – If the key is not found in the dictionary.

See also

FrozenDict.pop

Pop method for FrozenDict.

Examples

>>> from brainstate.util import FrozenDict, pop

>>> # Works with FrozenDict
>>> fd = FrozenDict({'a': 1, 'b': 2})
>>> fd2, value = pop(fd, 'a')
>>> value
1
>>> 'a' in fd2
False

>>> # Also works with regular dict
>>> d = {'a': 1, 'b': 2}
>>> d2, value = pop(d, 'a')
>>> value
1
>>> 'a' in d2
False