map_nest_at_index#

ivy.map_nest_at_index(nest, index, fn, /, shallow=True, _result=None)[source]#

Map a function to the value of a nested item at a specified index.

Parameters:
  • nest (Union[Array, NativeArray, Container, Dict, List]) – The nested object to update.

  • index (Sequence[Union[str, int]]) – A linear sequence of indices for the index at which to update.

  • fn (Callable[[Any], Any]) – The function to perform on the nested value at the given index.

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

  • _result (Optional[Union[Array, NativeArray, Container, Dict, List]], default: None) – Placeholder for the result of the update. do not set this parameter.

Return type:

Union[Array, NativeArray, Container, Dict, List, Tuple]

Returns:

ret – nest with applicable of fn on given index.

Examples

With ivy.Array inputs:

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> y = (1, 1)
>>> z = lambda a: a + 1.
>>> ivy.map_nest_at_index(x, y, z)
>>> print(x)
ivy.array([[1., 2.], [3., 5.]])
>>> x = ivy.array([1., 2., 3., 4.])
>>> y = [1]
>>> z = lambda a: a + 3.
>>> ivy.map_nest_at_index(x, y, z)
>>> print(x)
ivy.array([1., 5., 3., 4.])

With Dict input:

>>> x = {1 : [1, [2, 3]], 2: (4, 5)}
>>> y = (1, 1)
>>> z = lambda _: 2
>>> ivy.map_nest_at_index(x, y, z)
>>> print(x)
{1: [1, 2], 2: (4, 5)}

With List inputs:

>>> x = [['a', 'b', 'c'],
...      ['d', 'e', 'f'],
...      ['g', ['h', 'i']]]
>>> y = (2, 1, 0)
>>> z = lambda a: a + 'H'
>>> ivy.map_nest_at_index(x, y, z)
>>> print(x)
[['a','b','c'],['d','e','f'],['g',['hH','i']]]

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([1., 2.]) , b=ivy.array([4., 5.]))
>>> y = ('b',)
>>> z = lambda _: ivy.array([3., 4.])
>>> ivy.map_nest_at_index(x, y, z)
>>> print(x)
{
    a: ivy.array([1., 2.]),
    b: ivy.array([3., 4.])
}