Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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, such as a matrix of images (height, width, depth) or storing data for multiple 2D grids.
In this article, we will learn how to create a 3D list. While Python doesn't have built-in support for multi-dimensional arrays like other programming languages, we can create and manipulate 3D lists using nested lists and loops.
Using Nested Loops
The most straightforward way to create a 3D list is using nested loops. In the following example, we create a 3D list with sequential numbers starting from 1 ?
# Create a 3D list with dimensions 2x2x3
dimensions = (2, 2, 3)
x = 1
result = []
for i in range(dimensions[0]):
layer = []
for j in range(dimensions[1]):
row = []
for k in range(dimensions[2]):
row.append(x)
x += 1
layer.append(row)
result.append(layer)
print("3D list with sequential numbers:")
print(result)
3D list with sequential numbers: [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
Using List Comprehension
Python list comprehension offers a more concise way to create lists in a single line. It combines loops and conditional statements, making it more efficient than traditional loop methods.
Syntax
newlist = [expression for item in iterable if condition == True]
For a 3D list, we use nested list comprehensions. Here's an example creating a 2×3×4 list filled with zeros ?
# Create a 3D list of size 2x3x4 filled with zeros
x, y, z = 2, 3, 4
result = [[[0 for _ in range(z)] for _ in range(y)] for _ in range(x)]
print("3D list filled with zeros:")
print(result)
3D list filled with zeros: [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Using NumPy (Alternative Approach)
For more complex operations, NumPy provides better support for multi-dimensional arrays ?
import numpy as np
# Create a 3D array using NumPy
arr_3d = np.zeros((2, 3, 4), dtype=int)
print("3D array using NumPy:")
print(arr_3d)
# Create with sequential numbers
arr_sequential = np.arange(1, 25).reshape(2, 3, 4)
print("\n3D array with sequential numbers:")
print(arr_sequential)
3D array using NumPy: [[[0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0]]] 3D array with sequential numbers: [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Nested Loops | High | Slower | Learning and simple cases |
| List Comprehension | Medium | Faster | Pythonic, compact code |
| NumPy | High | Fastest | Mathematical operations |
Conclusion
Python offers multiple ways to create 3D lists: nested loops for clarity, list comprehension for conciseness, and NumPy for advanced mathematical operations. Choose the method that best fits your specific use case and performance requirements.
