class mergedicts(*args, _strict=False, _overwrite=True, _copy=False, _sameclass=True, _die=True, **kwargs)[source]#

Small function to merge multiple dicts together.

By default, skips any input arguments that are None, and allows keys to be set multiple times. This function is similar to dict.update(), except it returns a value. The first dictionary supplied will be used for the output type (e.g. if the first dictionary is an odict, an odict will be returned).

Note that arguments start with underscores to avoid possible collisions with keywords (e.g. sc.mergedicts(dict(loose=True, strict=True), strict=False, _strict=True)).

Note: This function is similar to the “|” operator introduced in Python 3.9. However, sc.mergedicts() is useful for cases such as function arguments, where the default option is None but you will need a dict later on.

  • _strict (bool) – if True, raise an exception if an argument isn’t a dict

  • _overwrite (bool) – if False, raise an exception if multiple keys are found

  • _copy (bool) – whether or not to deepcopy the merged dictionary

  • _sameclass (bool) – whether to ensure the output has the same type as the first dictionary merged

  • _die (bool) – whether to raise an exception if something goes wrong

  • *args (list) – the sequence of dicts to be merged

  • **kwargs (dict) – merge these into the dict as well


d0 = sc.mergedicts(user_args) # Useful if user_args might be None, but d0 is always a dict
d1 = sc.mergedicts({'a':1}, {'b':2}) # Returns {'a':1, 'b':2}
d2 = sc.mergedicts({'a':1, 'b':2}, {'b':3, 'c':4}, None) # Returns {'a':1, 'b':3, 'c':4}
d3 = sc.mergedicts(sc.odict({'b':3, 'c':4}), {'a':1, 'b':2}) # Returns sc.odict({'b':2, 'c':4, 'a':1})
d4 = sc.mergedicts({'b':3, 'c':4}, {'a':1, 'b':2}, _overwrite=False) # Raises exception
New in version 1.1.0: “copy” argument
New in version 1.3.3: keywords allowed
New in version 2.0.0: keywords fully enabled; “_sameclass” argument
New in version 2.0.1: fixed bug with “_copy” argument