How do you access individual elements of an array in python?

I am using a function that returns array of several arrays let say A. I don't know how to access each individual element. Thus, I am thinking to rearrange A to a matrix of two arrays so I could access them. The other option is to rearrange A (can be of order 10**6) to a big sparse matrix, the problem here is again I don't know how to achieve this. I need to point out I want to avoid Forloop. I know the total length of A (here is 7) but don't know the length of each individual array. Here is an example of A

A = array([[array([0, 1, 4])],
            [array([0, 1, 2, 5])],
            [array([1, 2, 3, 6])],
            [array([2, 3, 7])],
            [array([0, 4, 5, 8])],
            [array([1, 4, 5, 6, 9])],
            [array([ 2,  5,  6,  7, 10])]])   

and here is the index I am trying to change it to

B = (array([0,0,0,1,1,1,1,2,2,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6]),
array([0,1,4,0,1,2,5,1,2,3,6,2,3,7,0,4,5,8,1,4,5,6,9,2,5,6,7, 10]))

If this is impossible to do. Can you help me see if I can access each individual element, for example is there a way I could do the following

A[4][2] = 5   

or

 A[2][3] = 6

thank you so much for your help.

Array Indexing means searching for elements in an array using the index (position) of elements for quick retrieval of information.

Getting Started with Array Indexing in Python

Python arrays are variables that consist of more than one element. In order to access specific elements from an array, we use the method of array indexing.

The first element starts with index 0 and followed by the second element which has index 1 and so on. NumPy is an array processing package which we will use further.

Let’s start this off with a few examples.

Indexing to retrieve the third element of the array

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2])
30

In the above example, there are five elements in the array. 10 has index 0, 20 has index 1, 30 has index 2, 40 has index 3 and 50 has index 4.

So to retrieve the third element of the array which is 30 we determined its index 2.

Indexing to retrieve the fifth element of the array

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[4])
50


Arithmetic Operations using Array Indexing

Let’s perform arithmetic operations on individual elements of an array using indexing.

1. Adding two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[1]+a[3])
60

2. Subtracting two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[4]-a[0])
40

3. Multiply two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2]*a[3])
1200

4. Divide two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2]/a[3])
0.75


Indexing 2D Arrays in Python

2-Dimensional arrays in Python can be accessed using value, row, and columns. The general syntax for accessing specific elements from a 2D array is as follows:

Syntax : < value > = < array > [ row , column ] 

Here, means the variable where the retrieved element from the array is stored. And [row, column] specifies the row and column index of the value.

Construct a 2D array and retrieve one element using array index.

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6]])
>>> print(a)
[[1 2 3]
 [4 5 6]]
>>> value=a[1,2]
>>> print(value)
6


Indexing 3D Arrays in Python

Following is the general syntax for accessing elements from a 3D array using index.

Syntax : array[first dimension, second dimension, third dimension]

Here the first, second and third numbers represent 1D, 2D and 3D respectively.

Construct a 3D array and retrieve one element using the array index.

>>> import numpy as np
>>> a= np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
>>> print(a[0, 1, 2])
6


Python Array Index (Multi-dimensional Arrays)

Indexing a multi-dimensional array is quite complex. Let us start with creating a simple multidimensional array. For creating a multidimensional array we will use reshape() and arange()methods.

  • The reshape() function takes a single argument that specifies the new shape of the array.
  • The arange() method is used in Numpy. It takes to start and end arguments and creates a single dimension array.

>>> import numpy as np
>>> arr=np.arange(10).reshape(5,2)
>>> print(arr)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

>>> import numpy as np
>>> arr=np.arange(12).reshape(2,2,3)
>>> print(arr[0:3])
[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]

>>> print(arr[1:5:2,::3])
[[[6 7 8]

Conclusion

This was in brief about array indexing in the Python programming language. Hope this article proves helpful. You can learn more about array slicing in Python here.

How do you access individual array elements?

To access an individual element of an array, use the name of the array name followed by the index of the element in square brackets. Array indices start at 0 and end at size-1: array_name[index]; accesses the index'th element of array_name starting at zero.

How do you extract an element from an array in Python?

extract() function returns elements of input_array if they satisfy some specified condition. Parameters : array : Input array. User apply conditions on input_array elements condition : [array_like]Condition on the basis of which user extract elements.

How do you access a specific element in a list in Python?

Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.

How do you find the specific element of an array?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. index: The particular index of the given array.