8 Top Python Array Operations

Here are the operations that you can do with Arrays. Just install Numpy in your machine to work with python arrays.

Check out how to download NUMPY, and install using the “import NumPy as np” command. After the installation, check Numpy installed correctly.


With Numpy I did a small experiment. You may aware that ‘NumPy’ is a python package used in analytics.

8 Python Array Operations

  1. Create an array
  2. Mathematical operations like-add, Subtract, Multiply, and Division you can do on arrays
  3. You can join two arrays
  4. You can Copy one array to other
  5. Read and Write of Array data onto Files
  6. You can create Structured Arrays
  7. Vectorization of array elements also possible
  8. Slicing an Array

1. Declare

Using Python array function, now I am defining an array

array()
>>>a = np.array([1,2,3,5])
>>>a
>>>type(a)

The type() function tells you the type of an array.

Create an array

How to get Dimension of an Array in Python. Explained with the best example.

Array dimensions

Also You May Like : Top Libraries You Need for Data Analytics


2. Addition, Subtraction, Multiply & Division

You can do Addition, Subtraction, Multiply, Division on arrays

Array Mathematical Operations.

You can also get the index of each element. Index starts with ‘0’. So, to get an element of the second index is ‘3’.

index of an array

3. JOIN Two Arrays

You can join arrays either horizontal stacking or vertical stacking. Use vstack() or hstack().

joining the arrays

4. COPY Array to Array

You can copy data of one array to another. Explained the best example.

copy of an array to another

5. COPY Array to File

You can write data of an array into a file. There are two functions. One is save() and the other one is load().

The file which is saved will have the extension’.npy’

How to Save Array data to a file

6. Structured Array

Creating more complex arrays is called “structured arrays”. In an array, you can give of value with any data type.

structured arrays

7. Array Vectorization

Vectorization is the absence of an explicit loop during the development of the code. These loops actually cannot be omitted, but are implemented internally and then are replaced by other constructs in the code.

The application of vectorization leads to a more concise and readable code, and you can say that it will appear more “Pythonic” in its appearance.

In fact, thanks to the vectorization, many operations take on a more mathematical expression. For example, NumPy allows you to express the multiplication of two arrays as shown:

a * b
Or even two matrices:
A * B

8. Slicing an Array

I want to tell you the best example. Suppose you created an array of 4×4. You can split the same array as 2×2. This is called change of array dimension.

splitting arrays

Related Posts

Python Interview Questions

Don’t Forget to Share This Post!

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.