Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Multidimensional Arrays in Python: A Complete Guide

  • Tanishka DhondgeTanishka Dhondge
  • February 27, 2023
  • Python Array
Multidimensional Arrays in Python: A Complete Guide

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:

  • numpy.ones
  • numpy.zeros
  • numpy.identity

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

Tanishka Dhondge
Tanishka Dhondge
Articles: 59
Gradient descent algorithm with implementation from scratch
Previous Post Gradient descent algorithm with implementation from scratch
Next Post What does the "w" mean in open(filename, "w")?
What does the “w” mean in open(filename, “w”)?

Related Posts

Add Elements to Array Python

Add Elements to Array Python

  • November 22, 2026
How to Sort Array in Python

How to Sort Array in Python

  • November 19, 2026
Python Two Dimensional Array: Create and Use

Python Two Dimensional Array: Create and Use

  • November 16, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.