set_nest_at_indices#

ivy.set_nest_at_indices(nest, indices, values, /, shallow=True)[source]#

Set the value of a nested item at specified indices with specified values.

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

  • indices (Union[List[int], Tuple[int], Iterable[int]]) – A tuple of tuples of indices for the indices at which to update.

  • values (Union[List[int], Tuple[int], Iterable[int]]) – The new values for updating.

  • 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 updated values at the given indices.

Examples

With List inputs:

>>> nest = [[1, 2, 3, 4, 5, 6], ['a', 'b', 'c', 'd', 'e', 'f']]
>>> indices = [[0, 4], [1, 3]]
>>> values = [111, 'x']
>>> ivy.set_nest_at_indices(nest, indices, values)
>>> print(nest)
[[1, 2, 3, 4, 111, 6], ['a', 'b', 'c', 'x', 'e', 'f']]

With Tuple inputs:

>>> nest = [['abc', 'xyz', 'pqr'],[1, 4, 'a', 'b']]
>>> indices = ((0, 1),(1, 2))
>>> values = ('ivy', 'x')
>>> ivy.set_nest_at_indices(nest, indices, values)
>>> print(nest)
(['abc', 'ivy', 'pqr'], [1, 4, 'x', 'b'])

With Dict input:

>>> nest = {'a': [1., 2., 3.], 'b': [4., 5., 6.], 'c': [0.]}
>>> indices = (('a', 1), ('b', 2), ('c', 0))
>>> values = (11., 22., 33.)
>>> ivy.set_nest_at_indices(nest, indices, values)
>>> print(nest)
{'a': [1.0, 11.0, 3.0], 'b': [4.0, 5.0, 22.0], 'c': [33.0]}

With ivy.Array inputs:

>>> nest = ivy.array([[1., 2., 3.],[4., 5., 6.]])
>>> indices = ((0, 1),(1, 2))
>>> values = (11., 22.)
>>> ivy.set_nest_at_indices(nest, indices, values)
>>> print(nest)
ivy.array([[1., 11., 3.], [4., 5., 22.]])