brainstate.util.pop

Contents

brainstate.util.pop#

brainstate.util.pop(x, key)[source]#

Remove and return an item from a dictionary.

Overloads:

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

Parameters:
  • x (FrozenDict[K, V] | dict[K, V]) – Dictionary to pop from.

  • key (K) – Key to remove.

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