Nest#

Collection of Ivy functions for nested objects.

ivy.all_nested_indices(nest=None, /, include_nests=False, _index=None, _base=True)[source]#

Return indices of all the elements in nest.

Parameters:
  • nest (Optional[Union[List, Tuple, Dict, Array, NativeArray, Container]], default: None) – The nest to check the leaves of.

  • include_nests (bool, default: False) – Whether to also include indices of the nests themselves, not only leaves. Default is False.

  • _index (Optional[Union[int, Sequence[int]]], default: None) – The indices detected so far. None at the beginning. Used internally, do not set manually.

  • _base (bool, default: True) – Whether the current function call is the first function call in the recursive stack. Used internally, do not set manually.

Return type:

List

Returns:

ret – A set of indices of all elements in nest

Examples

With List input:

>>> x = [189, [863, 672], [264, 384]]
>>> y = ivy.all_nested_indices(x)
>>> print(y)
[[0], [1, 0], [1, 1], [2, 0], [2, 1]]

With Tuple input:

>>> x = (189, (863, 672), (264, 384))
>>> y = ivy.all_nested_indices(x, include_nests=True)
>>> print(y)
[[0], [1, 0], [1, 1], [1], [2, 0], [2, 1], [2]]

With Dict input:

>>> x = {'a': 2., 'b': [6., [15., 9.]], 'c': (7., 56.)}
>>> y = ivy.all_nested_indices(x)
>>> print(y)
[['a'], ['b', 0], ['b', 1, 0], ['b', 1, 1], ['c', 0], ['c', 1]]

With ivy.Array input:

>>> x = ivy.array([[True, False], [False, False]])
>>> y = ivy.all_nested_indices(x)
>>> print(y)
[[]]

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([412, 948, 482]), b=ivy.array([168, 674, 341]))
>>> y = ivy.all_nested_indices(x)
>>> print(y)
[['a'], ['b']]
ivy.copy_nest(nest, /, include_derived=False, to_mutable=False)[source]#

Copy a nest deeply, but without copying leaves of the nest, only the nest lists, tuples and dicts are copied.

Parameters:
  • nest (Union[Array, NativeArray, Iterable]) – The nest to copy.

  • include_derived (bool, default: False) – Whether to also recursive for classes derived from tuple, list and dict. Default is False.

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

Return type:

Union[Array, NativeArray, Iterable]

Returns:

ret – The copied nest.

Examples

With ivy.Array input:

>>> nest = ivy.array([[1.,2.,3.],[7.,8.,9.]])
>>> copied_nest = ivy.copy_nest(nest)
>>> print(copied_nest)
ivy.array([[1., 2., 3.],
        [7., 8., 9.]])

With Iterable input:

>>> nest = [[1, 2, 3, 4, 5], [23, 24, 25, 26, 27]]
>>> copied_nest = ivy.copy_nest(nest, include_derived = True)
>>> print(copied_nest)
[[1, 2, 3, 4, 5], [23, 24, 25, 26, 27]]
>>> nest = ([23, 25, 1337], [63, 98, 6])
>>> copied_nest = ivy.copy_nest(nest, to_mutable = True)
>>> print(copied_nest)
[[23, 25, 1337], [63, 98, 6]]
>>> nest = {'first': [23., 24., 25], 'second': [46., 48., 50]}
>>> copied_nest = ivy.copy_nest(nest)
>>> print(copied_nest)
{'first': [23.0, 24.0, 25], 'second': [46.0, 48.0, 50]}
ivy.duplicate_array_index_chains(nest)[source]#

Group all unique index chains in a nest. This function is useful for finding all unique index chains in a nest, and then duplicating the values at those index chains for functional frameworks.

Parameters:

nest (Union[Array, NativeArray, Iterable]) – nest to get duplicate index chains for.

Returns:

list of index chains to duplicate.

ivy.index_nest(nest, index, /)[source]#

Index a nested object, using a tuple of indices or keys in the case of dicts.

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

  • index (Union[List[int], Tuple[int], Iterable[int]]) – A tuple of indices for indexing.

Return type:

Any

Returns:

ret – The result element through indexing the nested object.

Examples

With Tuple inputs:

>>> x = (1, 2)
>>> y = [0]
>>> z = ivy.index_nest(x, y)
>>> print(z)
1

With ivy.Array inputs:

>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> y = [1]
>>> z = ivy.index_nest(x, y)
>>> print(z)
ivy.array([3., 4.])

With ivy.Container inputs:

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

With Dict input:

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

With List inputs:

>>> x = [['a', 'b', 'c'],
...      ['d', 'e', 'f'],
...      ['g', ['h', 'i']]]
>>> y = iter([2, 1, 0])
>>> z = ivy.index_nest(x, y)
>>> print(z)
h
ivy.insert_into_nest_at_index(nest, index, value)[source]#

Recursively inserts a value into a nested data structure at a specified index.

This function traverses a nested data structure and inserts the provided value at the specified index.

Parameters:
  • nest (Iterable) – The nested data structure.

  • index (Tuple) – The index specifying the location where the value should be inserted.

  • value (object) – The value to be inserted.

Return type:

None

Returns:

None

Examples

>>> nest = [[1, 2], [3, 4]]
>>> index = (1, 1)
>>> value = 99
>>> ivy.insert_into_nest_at_index(nest, index, value)
>>> print(nest)
[[1, 2], [3, 99, 4]]
ivy.insert_into_nest_at_indices(nest, indices, values, /)[source]#

Insert a value into the nested item at specified indices with specified values.

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

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

  • values – The new values for inserting.

Return type:

None

ivy.map(fn, constant=None, unique=None, mean=False)[source]#

Apply a function on each item of an iterable x.

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

  • constant (Optional[Dict[str, Any]], default: None) – keyword arguments which remain constant between each function call. Default is None.

  • unique (Optional[Dict[str, Iterable[Any]]], default: None) – keyword arguments which are unique for each function call. Default is None.

  • mean (bool, default: False) – Whether to compute the mean across the return values, and return this mean. Default is False.

Return type:

List

Returns:

ret – x following the application of fn to each of its iterated items.

Examples

With int inputs:

>>> def special_square(x : float) -> float : return np.square(x)
>>> results = ivy.map(fn = special_square,
...                   constant = None,
...                   unique = {'x' : [1,2,3]},
...                   mean = False)
>>> print(results)
[1, 4, 9]
>>> results = ivy.map(fn = special_square,
...                   constant = None,
...                   unique = {'x':[0,1,2]},
...                   mean = True)
>>> print(results)
1.6666666666666667
>>> def special_pow(x:float,y:float) ->float : return np.power(x,y)
>>> results = ivy.map(fn = special_pow,
...                   constant = {'y':[0,1]},
...                   unique = {'x':[1,2,3]},
...                   mean = False)
>>> print(results)
[array([1,1]),
array([1,2]),
array([1,3])]
>>> results = ivy.map(fn = special_pow,
...                   constant = {'y':[0,1]},
...                   unique = {'x':[1,2,3]},
...                   mean = True)
>>> print(results)
[1. 2.]

With float inputs:

>>> def linear_model(w:float, x:float, b:float) -> float: return w*x + b
>>> results = ivy.map(fn = linear_model,
...                   constant = {'w':10., 'b':1.},
...                   unique = {'x':[0.,1.,2.]},
...                   mean = False)
>>> print(results)
[1.0, 11.0, 21.0]

With ivy.Array inputs:

>>> results = ivy.map(fn = linear_model,
...    constant = {'w':ivy.array([1.,0.,1.]), 'b':ivy.array([0.,10.,100.])},
...    unique = {'x':[ivy.array([0.,1.,0.]), ivy.array([1.,1.,1.])]},
...    mean = False)
>>> print(results)
[ivy.array([0., 10., 100.]),
ivy.array([1., 10., 101.])]
>>> results = ivy.map(fn = linear_model,
...    constant = {'w':ivy.array([1.,0.,1.]), 'b':ivy.array([0.,10.,100.])},
...    unique = {'x':[ivy.array([0.,1.,0.]), ivy.array([1.,1.,1.])]},
...    mean = True)
>>> print(results)
ivy.array([  0.5,  10. , 100. ])
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.])
}
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.]])
ivy.multi_index_nest(nest, indices, /)[source]#

Repeatedly index a nested object, using a tuple of tuples of indices or keys in the case of dicts.

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

  • indices (Iterable[Iterable[int]]) – A tuple of tuples of indices to apply.

Return type:

Iterable[Any]

Returns:

ret – The result elements through indexing the nested object.

Examples

With Tuple inputs:

>>> x = (1, 2)
>>> y = [[0]]
>>> z = ivy.multi_index_nest(x, y)
>>> print(z)
[1]

With ivy.Array inputs:

>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> y = [[0],[1]]
>>> z = ivy.multi_index_nest(x, y)
>>> print(z)
[ivy.array([1., 2.], ivy.array([3., 4.])]

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([1,2]),
...                   b=[30,40])
>>> y = ('a', ('b', 0))
>>> z = ivy.multi_index_nest(x, y)
>>> print(z)
[ivy.array([1, 2]), 30]

With Dict input:

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

With List inputs:

>>> x = [['a', 'b', 'c'],
...      ['d', 'e', 'f'],
...      ['g', ['h', 'i']]]
>>> y = [[2, 1, 0], [0, 1]]
>>> z = ivy.multi_index_nest(x, y)
>>> print(z)
['h', 'b']
ivy.nested_any(nest, fn, check_nests=False, _base=True)[source]#

Check the leaf nodes of nest x via function fn, and returns True if any evaluate to True, else False.

Parameters:
  • nest (Iterable) – The nest to check the leaves of.

  • fn (Callable) – The condition function, returning True or False.

  • check_nests (bool, default: False) – Whether to also check the nests for the condition, not only nest leaves. Default is False.

  • _base (bool, default: True) – Whether the current function call is the first function call in the recursive stack. Used internally, do not set manually.

Return type:

bool

Returns:

ret – A boolean, whether the function evaluates to true for any leaf node.

ivy.nested_argwhere(nest, fn, check_nests=False, to_ignore=None, _index=None, _base=True, stop_after_n_found=None)[source]#

Check the leaf nodes of nested x via function fn, and returns all nest indices where the method evaluates as True.

Parameters:
  • nest (Iterable) – The nest to check the leaves of.

  • fn (Callable) – The condition function, returning True or False.

  • check_nests (bool, default: False) – Whether to also check the nests for the condition, not only nest leaves. Default is False.

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

  • _index (Optional[List], default: None) – The indices detected so far. None at the beginning. Used internally, do not set manually.

  • _base (bool, default: True) – Whether the current function call is the first function call in the recursive stack. Used internally, do not set manually.

  • stop_after_n_found (Optional[int], default: None) – to stop after some needed indices are found.

Return type:

Union[Iterable, bool]

Returns:

ret – A set of indices for the nest where the function evaluated as True.

Examples

With List input:

>>> nest = [[[1, -2, 3], 19], [[9, -36, 80], -10.19]]
>>> fun = ivy.abs
>>> nested_indices = ivy.nested_argwhere(nest, fn=fun)
>>> print(nested_indices)
[
    [0, 0, 0], [0, 0, 1],
    [0, 0, 2], [0, 1],
    [1, 0, 0], [1, 0, 1],
    [1, 0, 2], [1, 1]
]

With Tuple input:

>>> nest = ([-5, 9, 2], [0.3, 4.])
>>> fun = ivy.abs
>>> nested_indices = ivy.nested_argwhere(nest, fn=fun, stop_after_n_found=4)
>>> print(nested_indices)
[[0, 0], [0, 1], [0, 2], [1, 0]]

With Dict input:

>>> nest={'a': [2., 0.6, -2.], 'b': [1., 4., 1.9], 'c': [9.4]}
>>> fun = ivy.abs
>>> nested_indices = ivy.nested_argwhere(nest, fn=fun)
>>> print(nested_indices)
[
    ['a', 0], ['a', 1],
    ['a', 2], ['b', 0],
    ['b', 1], ['b', 2],
    ['c', 0]
]
ivy.nested_map(fn, x, /, include_derived=None, to_ignore=None, to_mutable=False, _tuple_check_fn=None, _list_check_fn=None, _dict_check_fn=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:
  • fn (Callable) – The function to map onto x.

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

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

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

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

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

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

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

  • 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, Iterable, Dict]

Returns:

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

Examples

With Tuple inputs:

>>> x = ([[1., 2.], [3., 4.]])
>>> function = lambda a : a * 2
>>> ivy.nested_map(function, x)
[[2.0, 4.0], [6.0, 8.0]]
>>> print(x)
[[2.0, 4.0], [6.0, 8.0]]

With Dict input:

>>> x = {1 : [1, [2, 3]], 2: (4, 5)}
>>> function = lambda a : a + 1
>>> ivy.nested_map(function, x)
{1 : [2, [3, 4]], 2: (5, 6)}
>>> print(x)
{1 : [2, [3, 4]], 2: (5, 6)}

With List inputs:

>>> x = [['a', 'b', 'c'],
...      ['d', 'e', 'f'],
...      ['g', ['h', 'i']]]
>>> function = lambda a: a + 'H'
>>> ivy.nested_map(function, x)
[['aH','bH','cH'],['dH','eH','fH'],['gH',['hH','iH']]]
>>> print(x)
[['aH','bH','cH'],['dH','eH','fH'],['gH',['hH','iH']]]

With ivy.Container input:

>>> x = ivy.Container(
...   a=ivy.array([[1, 2, 3], [9, 8, 7]]) , b=ivy.array([[4, 5, 6], [12, 13, 14]])
... )
>>> function = lambda a : a  + 1
>>> ivy.nested_map(function, x)
>>> print(x)
{
    a: ivy.array([[1, 2, 3],
                  [9, 8, 7]]),
    b: ivy.array([[4, 5, 6],
                  [12, 13, 14]])
}
>>> nest = ([1, 2], [3, 4], [5, 6], {"a": 1, "b": 2, "c": 3})
>>> function = lambda a :  a * 2
>>> ivy.nested_map(function, nest, to_ignore=list)
([1, 2, 1, 2], [3, 4, 3, 4], [5, 6, 5, 6], {'a': 2, 'b': 4, 'c': 6})
>>> nest = ([23, 25, 1337], [63, 98, 6])
>>> function = lambda a :  a + 1
>>> ivy.nested_map(function, nest, to_mutable=True)
[[24, 25, 1338], [64, 99, 7]]
ivy.nested_multi_map(func, nests, index_chains=None, to_apply=True, prune_unapplied=False, index_chain='', config=None, to_ivy=True)[source]#

Apply function to all array values from a collection of identically structured ivy arrays.

Parameters:
  • func (Callable) – Function to apply to each nest entry.

  • nest – nests to map.

  • index_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to index_chains, otherwise index_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune index_chains for which the function was not applied, otherwise the leftmost nest value is used. Default is False.

  • index_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

  • config (default: None) – The configuration for the nests. Default is the same as nest0.

  • to_ivy (default: True) – convert the output to ivy_arrays. Default is True

Returns:

nest containing the result of the function. The structure of the output is the same as the input with the result of the function applied to each applicable leaf and the value at that leaf in the first nest for a non-applicable leaf if prune_unapplied is False else unapplied leaves are pruned.

ivy.prune_empty(nest)[source]#

Prune empty nests from a nest.

Parameters:

nest – nest to prune.

Returns:

pruned nest with all empty nests removed

ivy.prune_nest_at_index(nest, index, /)[source]#

Prune a nested object at a specified index.

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

  • index (Tuple) – A tuple of indices for the index at which to prune.

Return type:

None

ivy.prune_nest_at_indices(nest, indices, /)[source]#

Prune a nested object at specified indices.

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

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

Return type:

None

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.])
}
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.]])

This should have hopefully given you an overview of the nest submodule, if you have any questions, please feel free to reach out on our discord in the nest channel!