map_nest_at_indices#

ivy.map_nest_at_indices(nest, indices, fn, /, shallow=True)[source]#

Map a function to the values of a nested item at the specified indices.

Parameters:
  • nest (Iterable) – The nested object to update.

  • indices (Tuple) – A tuple of tuples of indices for the indices at which to update.

  • fn (Callable) – The function to perform on the nest 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.

Return type:

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

Returns:

ret – nest with applicable of fn on given indices.

Examples

With List inputs:

>>> nest = [['a', 'c', 'e', 'd', 'u', 'k'], ['m', 'n', 'f', 'p', 'q', 't']]
>>> indices = [[0, 4], [1, 5]]
>>> function = lambda x : x + 'b'
>>> ivy.map_nest_at_indices(nest, indices, function)
>>> print(nest)
[['a', 'c', 'e', 'd', 'ub', 'k'], ['m', 'n', 'f', 'p', 'q', 'tb']]

With Tuple inputs:

>>> nest = ([-9, 8, -27],[9, -4, -5, 7])
>>> indices = ((0, 2),(1, 0),(1, 2))
>>> function = abs
>>> ivy.map_nest_at_indices(nest, indices, function)
>>> print(nest)
([-9, 8, 27], [9, -4, 5, 7])

With Dict input:

>>> nest = {'a': [8., 16., 22.], 'b': [10., 44., 81.], 'c': [9., 75., 37.]}
>>> indices = (('a', 2), ('b', 0), ('c', 1))
>>> function = lambda x : x + 1
>>> ivy.map_nest_at_indices(nest, indices, function)
>>> print(nest)
{'a': [8.0, 16.0, 23.0], 'b': [11.0, 44.0, 81.0], 'c': [9.0, 76.0, 37.0]}

With ivy.Array inputs:

>>> nest = ivy.array([[-9., 8., -17.],[11., -3., 5.]])
>>> indices = ((0, 1),(1, 1),(1, 2))
>>> function = lambda x : x ** 2
>>> ivy.map_nest_at_indices(nest, indices, function)
>>> print(nest)
ivy.array([[ -9.,  64., -17.],
       [ 11.,   9.,  25.]])