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
🌐
Python Guides
pythonguides.com › python-numpy-3d-array
3D Arrays In Python Using NumPy
May 16, 2025 - In this article, I’ll share several ... in Python, focusing primarily on NumPy which is the gold standard for multidimensional array operations. We’ll explore everything from basic creation methods to advanced slicing techniques. ... A 3D array is essentially a collection of 2D arrays stacked on top of each other. Think of it as a cube of data with three axes: depth, rows, and columns. For example, you might ...
🌐
w3resource
w3resource.com › python-exercises › list › python-data-type-list-exercise-13.php
Python: Generate a 3D array - w3resource
So, the resulting array is a 3D array of size 3x4x6, with every element initialized to the character '*'. Finally print() function prints the said array. ... Write a Python program to generate a 3D array where each element is its coordinate sum.
🌐
Python Examples
pythonexamples.org › python-numpy-create-3d-array
Create 3D Array in NumPy
The function returns a numpy array with specified shape. import numpy as np # create a 3D array with shape (2, 3, 4) shape = (2, 3, 4) arr = np.empty(shape) print(arr)
🌐
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
print(nums): Prints the created 3-dimensional array. ... Construct a 3D array of shape (3,5,4) filled with sequential integers and then reverse the order of its 2D subarrays.
🌐
GeeksforGeeks
geeksforgeeks.org › python-creating-3d-list
Python - Creating a 3D List - GeeksforGeeks
December 11, 2024 - We are going to use matplotlib and mplot3d to plot the 3D Heatmap in Python. We need to ins ... In Python list() constructor is a built-in function which construct a list object. We can use list constructor to create an empty list or convert an iterable (dictionary, tuple, string etc.) to a list. Python# Example of list constructor a = list() # creates an empty list b = list((1, 2, 3)) # cover
🌐
AskPython
askpython.com › home › multidimensional arrays in python: a complete guide
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - 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) ...
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy 3d array
NumPy 3D array | Learn the Examples of NumPy 3D array
April 15, 2023 - Here we discuss the concept of NumPy 3D array in Python through definition, syntax, and declaration of the 3D array in Python through programming examples and their outputs.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › 3d arrays in python
3d Arrays in Python | How to Create,Insert And Remove 3D Array In Python
April 23, 2024 - Guide to 3d Arrays in Python. Here we discuss the introduction and how 3D Arrays are defined in Python along with creation, insertion arrays.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Delft Stack
delftstack.com › home › howto › python › declare 3d array in python
How to Declare 3D Array in Python | Delft Stack
February 2, 2024 - The list comprehension method works fine, but it is a bit code extensive. If we want to minimize our code, we can use another approach called the multiplication method. The following code example shows us how to use the multiplication method to declare a 3D array in Python.
🌐
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:
🌐
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 - There are three ways to construct ... Using the array() function array = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]) # Using the reshape() function numbers = ...
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".

🌐
Programming Idioms
programming-idioms.org › idiom › 27 › create-a-3-dimensional-array › 192 › python
Create a 3-dimensional array, in Python
-spec array(pos_integer(), pos_integer(), pos_integer()) -> [[[float()]]]. array(M, N, P) -> [array(M, N) || _ <- lists:seq(1, P)]. array(M, N) -> [array(M) || _ <- lists:seq(1, N)]. array(M) -> [rand:uniform() || _ <- lists:seq(1, M)].
🌐
EyeHunts
tutorial.eyehunts.com › home › 3d array python
3D array Python
July 3, 2023 - Here’s an example of a simple 3D array with dimensions 2x3x4: [ [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ], [ [13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24] ] ] In Python, you can create a 3D array using nested lists or by using ...
🌐
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 - A 3D array is a collection of tables stacked on top of each other. ... Each word in a line is a column. So a 3D array consists of layers → rows → columns. For example, a 3D array with the shape 2 x 3 x 4 means:
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › reshape_and_3d.html
Reshaping and three-dimensional arrays — Functional MRI methods
>>> arr_1d_bigger = np.arange(24) >>> arr_1d_bigger array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) >>> arr_1d_bigger.shape (24,) >>> arr_3d = arr_1d_bigger.reshape((2, 3, 4)) >>> arr_3d array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], ...
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-create-3d-list
Python program to create 3D list.
In Python, A 3D list is also called a three-dimensional array (list of lists of lists). It can be visualized as a cube or a set of tables stacked together. It is commonly used to represent data with three indices. For example, a matrix of images (height, width, depth).