nested_map#

ivy.nested_map(x, /, fn, include_derived=None, to_ignore=None, to_mutable=False, max_depth=None, _depth=0, _tuple_check_fn=None, _list_check_fn=None, _dict_check_fn=None, extra_nest_types=None, shallow=True)[source]#

Apply a function on x in a nested manner, whereby all dicts, lists and tuples are traversed to their lowest leaves before applying the method and returning x. If x is not nested, the method is applied to x directly.

Parameters:
  • x (Union[Array, NativeArray, Iterable]) – The item to apply the mapped function to.

  • fn (Callable) – The function to map onto x.

  • include_derived (Optional[Union[Dict[type, bool], bool]]) – Whether to also recursive for classes derived from tuple, list and dict. (default: None) Default is False.

  • to_ignore (Optional[Union[type, Tuple[type]]]) – Types to ignore when deciding whether to go deeper into the nest or not (default: None)

  • to_mutable (bool) – Whether to convert the nest to a mutable form, changing all tuples to lists. (default: False) Default is False.

  • max_depth (Optional[int]) – The maximum nested depth to reach. Default is 1. Increase this if the nest is (default: None) deeper.

  • _depth (int) – Placeholder for tracking the recursive depth, do not set this parameter. (default: 0)

  • _tuple_check_fn (Optional[Callable]) – Placeholder for the tuple check function, do not set this parameter. (default: None)

  • _list_check_fn (Optional[Callable]) – Placeholder for the list check function, do not set this parameter. (default: None)

  • _dict_check_fn (Optional[Callable]) – Placeholder for the dict check function, do not set this parameter. (default: None)

  • extra_nest_types (Optional[Union[type, Tuple[type]]]) – Types to recursively check when deciding whether to go deeper into the (default: None) nest or not

  • shallow (bool) – Whether to inplace update the input nest or not (default: True) Only works if nest is a mutable type. Default is True.

Return type:

Union[Array, NativeArray, Iterable, Dict]

Returns:

ret – x following the applicable of fn to it’s nested leaves, or x itself if x is not nested.