index_nest#

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