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
🌐
w3resource
w3resource.com › python-exercises › list › python-data-type-list-exercise-13.php
Python: Generate a 3D array - w3resource
Python List Exercises, Practice and Solution: Write a Python program to generate a 3*4*6 3D array whose each element is *.
🌐
Python Guides
pythonguides.com › python-numpy-3d-array
3D Arrays In Python Using NumPy
May 16, 2025 - Learn how to work with 3D arrays in Python using NumPy. This comprehensive guide covers creation methods, indexing, slicing, and applications like image processing
🌐
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 ...
🌐
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
NumPy Basic Exercises, Practice and Solution: Write a NumPy program to create a three-dimension array with shape (3,5,4) and set to a variable.
🌐
AskPython
askpython.com › home › multidimensional arrays in python: a complete guide
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - 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
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_reshape.asp
NumPy Array Reshaping
Convert 1D array with 8 elements to 3D array with 2x2 elements:
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
Thus d3_array[0, 1, 0] specifies the element residing in sheet-0, at row-1 and column-0: # retrieving a single element from a 3D-array >>> d3_array[0, 1, 0] 2
Find elsewhere
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python-creating-3d-list
Python - Creating a 3D List - GeeksforGeeks
December 11, 2024 - Scalar, 1 and 2 dimensional inputs are converted to 3-dimensional arrays, whilst higher-dimensional inputs are preserved. Input includes scalar, lists, lists of tuples, tuples, tuples of tuple ...
🌐
Python Examples
pythonexamples.org › python-numpy-create-3d-array
Create 3D Array in NumPy
In this NumPy Tutorial, we learned how to create a 3D numpy array in Python using different NumPy functions.
🌐
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 - Guide to NumPy 3D array. Here we discuss the concept of NumPy 3D array in Python through definition, syntax, and declaration of the 3D array.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
EyeHunts
tutorial.eyehunts.com › home › 3d array python
3D array Python
July 3, 2023 - import numpy as np # Create a 3D array using NumPy array_3d = 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], [25, 26, 27]] ]) # Accessing elements in the 3D array print(array_3d[1, 2, 0]) # Output: 16 # Modifying elements in the 3D array array_3d[0, 1, 2] = 99 print(array_3d) ... Comment if you have any doubts or suggestions on this Python Array topic.
🌐
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 - If you’ve spent some time working with data in Python, you’re probably familiar with lists — maybe even nested lists that represent tables or matrices. But sometimes, two dimensions just aren’t enough. In fields like image processing, scientific computing, or machine learning, we often deal with data that goes one level deeper. That’s where three-dimensional arrays come in. In this post, we’ll explore what 3D arrays are, why we use them, and how to work with them effectively in Python — all without getting buried in code.
🌐
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.
🌐
W3Schools
w3schools.com › c › c_arrays_multi.php
C Multidimensional Arrays (Two-dimensional and more)
You can also declare arrays with more than two dimensions: // A 3D array with 2 blocks, each with 4 rows and 3 columns int example[2][4][3];
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".