Python - Creating a 3D List
Last Updated :
11 Dec, 2024
In Python, 3D list represents a three dimensional data structure where we can organize elements in three axes (rows, columns, and depth). Let’s explore various ways to create a 3D list in Python.
Using Nested List Comprehensions
Nested list comprehensions are a concise way to create 3D lists by iterating over ranges for each dimension.
Python
# Create a 3D list with dimensions 2x3x4, initialized to 0
a = [[[0 for k in range(4)] for j in range(3)] for i in range(2)]
print(a)
Output[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Explanation:
- The innermost loop creates a list of size
4
initialized to 0
. - The middle loop replicates this list
3
times to form a 2D list (rows × columns). - The outermost loop replicates the 2D list
2
times to form the 3D structure.
Let's see some more methods and see how we can create a 3D list in Python.
Using Loops
Using explicit nested loops, we can initialize a 3D list step by step. This method provides more control and is easier to debug.
Python
# Dimensions
x, y, z = 2, 3, 4
# Create 3D list
a = []
for i in range(x):
temp = []
for j in range(y):
temp.append([0] * z)
a.append(temp)
print(a)
Output[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Explanation:
- We first create a 2D list for each
i
iteration (outermost loop). - The inner loop appends rows to this 2D list.
Using NumPy
For numerical computations, using NumPy is more efficient and convenient.
Python
import numpy as np
# Create a 3D array with dimensions 2x3x4, initialized to 0
a = np.zeros((2, 3, 4))
print(a)
Explanation:
- The
np.zeros()
function creates an array filled with 0
with the specified dimensions. - NumPy is optimized for large scale computations and is faster compared to native Python lists.
Similar Reads
Extending a list in Python In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will
2 min read
Appending to 2D List in Python A 2D list in Python is a list of lists where each sublist can hold elements. Appending to a 2D list involves adding either a new sublist or elements to an existing sublist. The simplest way to append a new sublist to a 2D list is by using the append() method.Pythona = [[1, 2], [3, 4]] # Append a new
2 min read
Interesting facts about Python Lists Python lists are one of the most powerful and flexible data structures. Their ability to store mixed data types, support dynamic resizing and provide advanced features like list comprehension makes them an essential tool for Python developers. However, understanding their quirks, like memory usage a
6 min read
numpy.atleast_3d() in Python numpy.atleast_3d() function is used when we want to Convert inputs to arrays with at least three dimension. 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
2 min read
3D Heatmap in Python Heatmaps are a great way to visualize a dataset, methods for visualizing the data are getting explored constantly and 3D heatmap is one of the ways to plot data. Let's learn how we can plot 3D data in python. We are going to use matplotlib and mplot3d to plot the 3D Heatmap in Python. We need to ins
5 min read