You should use a list comprehension:

>>> import pprint
>>> n = 3
>>> distance = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][1]
[0, 0, 0]
>>> distance[0][1][2]
0

You could have produced a data structure with a statement that looked like the one you tried, but it would have had side effects since the inner lists are copy-by-reference:

>>> distance=[[[0]*n]*n]*n
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][0][0] = 1
>>> pprint.pprint(distance)
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
 [[1, 0, 0], [1, 0, 0], [1, 0, 0]],
 [[1, 0, 0], [1, 0, 0], [1, 0, 0]]]
Answer from robert on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › what is 3d array in python?
r/learnpython on Reddit: What is 3D array in python?
February 19, 2024 -

Hey everyone,

Currently I am learning arrays in python for learning machine learning, and I learned 1D array and 2D array now I want to learn 3D array, but I don't get any resource which explaining 3D arrays in python, I searched on google, Gemini, ChatGPT, Bing ai, YouTube. But anyone is not explaining 3D array properly,

Can anyone please Explain me 3D arrays and How 3D arrays look like?

Vectors (2d / 3d) in python? Jan 23, 2021
r/learnpython
5y ago
Best way to visualize a 3d numpy array? Feb 24, 2023
r/learnpython
3y ago
Mapping out surface of 3D object into an 3D array? Aug 19, 2023
r/GraphicsProgramming
3y ago
Don’t understand how 3D things are made Apr 21, 2024
r/learnpython
2y ago
More results from reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-creating-3d-list
Python - Creating a 3D List - GeeksforGeeks
December 11, 2024 - In Python, 3D list represents a three dimensional data structure where we can organize elements in three axes (rows, columns, and depth).
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
Let’s build up some intuition for arrays with a dimensionality higher than 2. The following code creates a 3-dimensional array: # a 3D array, shape-(2, 2, 2) >>> d3_array = np.array([[[0, 1], ... [2, 3]], ... ...
🌐
Quora
quora.com › How-can-you-create-an-array-3D-in-Python
How to create an array 3D in Python - Quora
Answer: In Python, you can create a 3D array using lists or, preferably, NumPy arrays. NumPy is a powerful library for numerical operations, and it provides convenient functions for working with multi-dimensional arrays. Here's how you can create a 3D array using NumPy:
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - 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. ... 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) ... Arrays are great data structures used to store homogenous data. the default array is more like a list data structure in python.
Find elsewhere
🌐
Facebook
facebook.com › groups › python › posts › 1339237983583429
Why is creating a 3D array in Python more complex?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
NumPy
numpy.org › doc › stable › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.5 Manual
The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray. As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array (using, for example, N integers), ...
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.5 Manual
Using np.newaxis will increase the dimensions of your array by one dimension when used once. This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-reshape-2d-to-3d-array
Numpy Reshape 2D To 3D Array - GeeksforGeeks
July 23, 2025 - To reshape a 2D NumPy array into a 3D array, you can use the reshape() method.
Top answer
1 of 6
71

You have a truncated array representation. Let's look at a full example:

>>> a = np.zeros((2, 3, 4))
>>> a
array([[[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]],

       [[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]]])

Arrays in NumPy are printed as the word array followed by structure, similar to embedded Python lists. Let's create a similar list:

>>> l = [[[ 0.,  0.,  0.,  0.],
          [ 0.,  0.,  0.,  0.],
          [ 0.,  0.,  0.,  0.]],

          [[ 0.,  0.,  0.,  0.],
          [ 0.,  0.,  0.,  0.],
          [ 0.,  0.,  0.,  0.]]]

>>> l
[[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], 
 [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]

The first level of this compound list l has exactly 2 elements, just as the first dimension of the array a (# of rows). Each of these elements is itself a list with 3 elements, which is equal to the second dimension of a (# of columns). Finally, the most nested lists have 4 elements each, same as the third dimension of a (depth/# of colors).

So you've got exactly the same structure (in terms of dimensions) as in Matlab, just printed in another way.

Some caveats:

  1. Matlab stores data column by column ("Fortran order"), while NumPy by default stores them row by row ("C order"). This doesn't affect indexing, but may affect performance. For example, in Matlab efficient loop will be over columns (e.g. for n = 1:10 a(:, n) end), while in NumPy it's preferable to iterate over rows (e.g. for n in range(10): a[n, :] -- note n in the first position, not the last).

  2. If you work with colored images in OpenCV, remember that:

    2.1. It stores images in BGR format and not RGB, like most Python libraries do.

    2.2. Most functions work on image coordinates (x, y), which are opposite to matrix coordinates (i, j).

2 of 6
27

No need to go in such deep technicalities, and get yourself blasted. Let me explain it in the most easiest way. We all have studied "Sets" during our school-age in Mathematics. Just consider 3D numpy array as the formation of "sets".

x = np.zeros((2,3,4)) 

Simply Means:

2 Sets, 3 Rows per Set, 4 Columns

Example:

Input

x = np.zeros((2,3,4))

Output

Set # 1 ---- [[[ 0.,  0.,  0.,  0.],  ---- Row 1
               [ 0.,  0.,  0.,  0.],  ---- Row 2
               [ 0.,  0.,  0.,  0.]], ---- Row 3 
    
Set # 2 ----  [[ 0.,  0.,  0.,  0.],  ---- Row 1
               [ 0.,  0.,  0.,  0.],  ---- Row 2
               [ 0.,  0.,  0.,  0.]]] ---- Row 3

Explanation: See? we have 2 Sets, 3 Rows per Set, and 4 Columns.

Note: Whenever you see a "Set of numbers" closed in double brackets from both ends. Consider it as a "set". And 3D and 3D+ arrays are always built on these "sets".

🌐
scikit-learn
scikit-learn.org › stable › modules › clustering.html
2.3. Clustering — scikit-learn 1.9.1 documentation
Each clustering algorithm comes in two variants: a class, that implements the fit method to learn the clusters on train data, and a function, that, given train data, returns an array of integer labels corresponding to the different clusters.
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-56.php
NumPy: Create a three-dimension array with shape (3,5,4) and set to a variable - w3resource
August 28, 2025 - Create a 3D array of the specified shape using random values and compute the cumulative sum along the third axis.
🌐
Medium
medium.com › @bouimouass.o › what-3d-arrays-look-like-some-ways-to-construct-them-and-their-applications-5f054ce9adb8
What 3D arrays look like, some ways to construct them and their applications? | by Omar | Medium
July 23, 2023 - Python · Design · Data Science · Omar · Follow · 3 min read · ·Jul 23, 2023 · 5 · 1 · Listen · Share · A 3D array is a three-dimensional array of data. It is a rectangular array with three dimensions: rows, columns, and slices.
🌐
Medium
medium.com › @girginlerheryerde › layer-by-layer-understanding-3d-arrays-in-python-a5709b7ef8d1
Layer by Layer: Understanding 3D Arrays in Python | by Ayşenas Girgin | Medium
March 17, 2025 - Whenever your data has structure across multiple axes, a 3D array helps you manage it logically and cleanly. In Python, a 3D array is usually just a list of lists of lists.
🌐
Towards Data Science
towardsdatascience.com › home › latest › the absolute beginner’s guide to pandas dataframes
The Absolute Beginner’s Guide to Pandas DataFrames | Towards Data Science
November 17, 2025 - Here, I’ve created a 2D Array. Pandas DataFrame can only store 1D and 2D arrays. If you try to pass in a 3D Array, you’ll get an error.