set_nest_at_index#

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

Set the value of a nested item at a specified index.

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

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

  • value (Any) – The new value 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.

  • _result (Optional[Union[List, Tuple, Dict, Array, NativeArray, Container]], 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 changed value at the given index.

Examples

With ivy.Array inputs:

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> y = (1, 1)
>>> z = 5.
>>> ivy.set_nest_at_index(x, y, z)
>>> print(x)
ivy.array([[1., 2.], [3., 5.]])
>>> x = ivy.array([1., 2., 3., 4.])
>>> y = [1]
>>> z = 5.
>>> ivy.set_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 = 2
>>> ivy.set_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 = 'H'
>>> ivy.set_nest_at_index(x, y, z)
>>> print(x)
[['a','b','c'],['d','e','f'],['g',['H','i']]]

With ivy.Container input:

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