Multidimensional Arrays in Python: A Complete Guide

Multidimentional Arrays

An array with multiple dimensions can represent relational tables and matrices and is made up of many one-dimensional arrays, multi-dimensional arrays are frequently used to store data for mathematical computations, image processing, and maintaining records.

In this article, the creation and implementation of multidimensional arrays (2D, 3D as well as 4D arrays) have been covered along with examples in Python Programming language. To understand and implement multi-dimensional arrays in Python, the NumPy package is used. It is a Python library that gives users access to a multidimensional array object, a variety of derived objects (such as masked arrays and matrices), and a selection of functions for quick operations on arrays and multi-dimensional matrices.

The standard way of Python language creates lists which are very similar to arrays but remember, there is a very slight difference between a List and an array in Python programming language. You can learn more about the differences between lists vs arrays in python. In this article let’s look purely at arrays. The following example will show the difference between the datatype of creating a 2D array created by the standard way and by using the Numpy package.

#creating 2D array without any package
arr1 = [[0]*3]*2
print(arr1)
print(type(arr1))

#creating 2D array with numpy package
print("\n")
arr2 = np.array([[1,2,3,4], [5,6,7,8]])
print(arr2)
print(type(arr2))

OUTPUT

List And Array
List And Array

Make sure to install and import the Numpy package in your current IDE before beginning with the array implementation to avoid any kind of errors. To do so run the following lines of code.

#To install the Numpy package
pip install numpy

#To import the Numpy package
import numpy as np

Two-dimensional (2D) array

An array of arrays is a simple definition that applies to the two-dimensional array. The rows and columns of the matrices that make up the 2D array’s organizational structure can be viewed as its primary component. The 2D array can be visualized as a table (a square or rectangle) with rows and columns of elements. The image below depicts the structure of the two-dimensional array.

2D Array
2D Array

Implementing 2D array in Python

Let’s start with implementing a 2 dimensional array using the numpy array method.

arr = np.array([array1 values..][array2 values...])
  • arr: array’s name
  • np.array: function of the Numpy package
array_1 = np.array([[1,2,3,4],
                    [5,6,7,8]])

print("Output")
print(array_1)

OUTPUT

2D Array Example
2D Array Example

Three-dimensional (3D) array in Python

A 3-D (three-dimensional) array is mainly composed of an array of 2-D arrays. The rows, columns, and page elements can be viewed as primary components of a 3D array. We can visualize it as multiple tables comprising of rows and columns attached (like a cube). The image below depicts the structure of the three-dimensional array.

3D Array 1
3D Array 1
arr = numpy.array([2D_array1 values],[2D_array2 values]...)
  • arr: array’s name
  • np.array: function of the Numpy package
array_2 = np.array([[[1,2,3],[3,4,5]],
                   [[6,7,8],[9,8,7]],
                   [[6,5,4],[3,2,1]]])

print("Output")
print(array_2)

OUTPUT

3D Array Example
3D Array Example

Four-dimensional (4D) array

A simple way to define a 4D array is that it’s an array of 3D arrays. It is a bit complicated to visualize a 4D array but we can say that it’s a set of 3D arrays (like a row of cubes). The image below depicts the structure of the four-dimensional array.

4D Array
4D Array

SYNTAX

arr = numpy.array([3D_array1],[3D_array2]…)

  • arr: array’s name
  • numpy.array: function of the numpy package
array_3 = np.array([[[[1,2],[3,4],[5,6]]],
                   [[[7,8],[9,8],[7,6]]],
                   [[[5,4],[3,2],[1,0]]]])
                   

print("Output")
print(array_3)

OUTPUT

4D Array Example
4D Array Example

Summary

Arrays are great data structures used to store homogenous data. the default array is more like a list data structure in python. Numpy is one of the applicable open-sourced free-to-use packages that help in creating and working on arrays faster and in a much more efficient manner. The Numpy package also many other built-in functions to manipulate and reshape multidimensional arrays. A few other built-in functions of the Numpy package to create multi-dimensional arrays are as follows:

To view more such detailed and easy-to-understand articles on Python programming language click here!