Last Updated:

Python arrays [ Matrix ] - How to use in operations?

Working with Python arrays has its own characteristics. In such a programming language as Python, arrays are usually understood as a certain sequence of data, the purpose of which is to store homogeneous objects.

In fact, arrays are the same lists, with a reservation for a number of restrictions, for example, on the input of data, their volume, etc. However, these restrictions do not prevent arrays from being a fairly convenient and functional way of storing data.

Python arrays matrix

Add a new array

Before the process of creating a new array, you must perform some steps. To begin with, it is worth importing the library that is responsible for working with such objects. To perform this action, you need to add the following line to the program file: from array import *.

Based on the fact that arrays are designed to work with one type of data, then, and, accordingly, the size of the cells of this data will also be the same.

To create a new data array, a function such as "array" is used. The following is an example of how to populate an array using the following steps:

from array import *
data = array(‘i’, [2, 5, 4, 0, 8])

The "array" function is able to take two arguments, one of them is the type of array that is created, the other is the original list of array values. In this example, i is a number whose size is 2 b. It is worth noting that you can use not only this primitive, but also others - c, f, etc.

Do not forget that in the array, as noted earlier, only one type of data can be stored. If this condition is not met, an error is thrown when starting the program. Square brackets [] are used to refer to an element.

Steps to add a new item


In order for a new element to appear in the array, it is necessary to use such a method as "insert". This is done by entering two values into the object that you created earlier, which are arguments. The digit 3 is nothing more than the value itself, and 4 indicates the place in the array where the element will be located, that is, its index.

Steps to delete a new item


In the programming language in question, you can get rid of unnecessary elements through such a method as "pop". This method has an argument (3) and can be called through an object that was created earlier, that is, in a manner similar to adding a new element.


After the excess has been removed, the array is shifted its contents so that the number of free memory cells coincides with the current number of elements.



Often there is a need to check the data when working with any program that is carried out by displaying on the screen. This operation can be performed using a command such as "print". The argument for this function is an element of the array created earlier.

In the example below, you can see that the array is processed using a "for" loop, in which any element of the array is with the identifier i to be passed to "print".

After performing these operations in the form of a column, all previously assigned values are displayed in the form of integers in turn.

Two-dimensional and multidimensional arrays

Often there are cases in which a one-dimensional array is too small to illustrate a particular information. Unfortunately, in the python programming language there is no possibility to create two-dimensional and multidimensional arrays, however, it is possible to create two-dimensional lists.

As you can see from the example, multiple "d2" lists are created in one large list "d1". Through two "for" cycles, two matrices with a size of 5 by 5 are automatically filled with zeros. The "append" method adds a new item (0) to the list, and the "range" method indicates its value (5).


It's also worth noting that each new "for" loop uses its own variable, which performs a representation of the current item of the external (j) or internal (i) lists. Specifying the desired cell in square brackets with a focus on columns and rows d1[1][2] allows you to access this cell from a multidimensional list.

On the same principle, a complex list is built in the form of a multidimensional array.



The Python programming language uses arrays to interact with datasets of a similar type. Manipulation of the contents of such a structure allows you to carry out the standard library of the platform. Moreover, this programming language supports a multidimensional representation of lists without restrictions on the number of levels.