from typing import OrderedDict


def to_native(key):
    """
    Given a mapping (e.g. dictionary) or a iterable (e.g. list) with 0d NumPy array or Torch tensor values,
    convert these into native Python data type object (e.g. ints and floats).

    Args:
        key: a mapping or an iterable object

    Returns:
        Same object as `key` but with 0d NumPy array or Torch tensor value replaced with native scalar value
    """
    if isinstance(key, (dict, OrderedDict)):
        for k, v in key.items():
            if hasattr(v, 'dtype') and len(v) < 2:
                key[k] = v.item()
    else:
        for k, v in enumerate(key):
            if hasattr(v, 'dtype') and len(v) < 2:
                key[k] = v.item()
    return key