A 3-D List means that we need to make a list that has three parameters to it, i.e., (a x b x c), just like a 3 D array in other languages. In this program we will try to form a 3-D List with its content as “#”. Lets look at these following examples:
Input :
3 x 3 x 3
Output :
[[['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']],
[['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']],
[['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']]]
Input :
5 x 3 x 2
Output :
[[['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#']],
[['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#']]]
Python3
import pprint
def ThreeD(a, b, c):
lst = [[ [ '#' for col in range (a)] for col in range (b)] for row in range (c)]
return lst
col1 = 5
col2 = 3
row = 2
pprint.pprint(ThreeD(col1, col2, row))
|
Output
[[['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#']],
[['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#']]]
Refer pprint() to get more insight into this topic. Now let’s suppose we need to merge two 3D lists into one.
Python3
import pprint
def ThreeD(a, b, c):
lst1 = [[ [ '1' for col in range (a)] for col in range (b)] for row in range (c)]
lst2 = [[ [ '2' for col in range (a)] for col in range (b)] for row in range (c)]
lst = lst1 + lst2
return lst
col1 = 3
col2 = 3
row = 3
pprint.pprint(ThreeD(col1, col2, row))
|
Output
[[['1', '1', '1'], ['1', '1', '1'], ['1', '1', '1']],
[['1', '1', '1'], ['1', '1', '1'], ['1', '1', '1']],
[['1', '1', '1'], ['1', '1', '1'], ['1', '1', '1']],
[['2', '2', '2'], ['2', '2', '2'], ['2', '2', '2']],
[['2', '2', '2'], ['2', '2', '2'], ['2', '2', '2']],
[['2', '2', '2'], ['2', '2', '2'], ['2', '2', '2']]]
Using nested for loops:
Approach:
In this approach, we can create a 3D list using nested for loops. We will start by creating an empty list and then using three for loops to iterate over the dimensions and append the ‘#’ character to the list.
Create an empty list lst to hold the 3D list.
Loop through the range x to create the first dimension of the 3D list.
Within the x loop, create an empty list lst_2d to hold the 2D list for each x element.
Loop through the range y to create the second dimension of the 3D list.
Within the y loop, create an empty list lst_1d to hold the 1D list for each y element.
Loop through the range z to create the third dimension of the 3D list.
Within the z loop, append the value ‘#’ to the 1D list lst_1d for each z element.
After the z loop, append the 1D list lst_1d to the 2D list lst_2d.
After the y loop, append the 2D list lst_2d to the 3D list lst.
After the x loop, return the 3D list lst.
Python3
def create_3d_list_1(x, y, z):
lst = []
for i in range (x):
lst_2d = []
for j in range (y):
lst_1d = []
for k in range (z):
lst_1d.append( '#' )
lst_2d.append(lst_1d)
lst.append(lst_2d)
return lst
x = 3
y = 3
z = 3
output_1 = create_3d_list_1(x, y, z)
print (output_1)
x = 5
y = 3
z = 2
output_2 = create_3d_list_1(x, y, z)
print (output_2)
|
Output
[[['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']], [['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']], [['#', '#', '#'], ['#', '#', '#'], ['#', '#', '#']]]
[[['#', '#'], ['#', '#'], ['#', '#']], [['#', '#'], ['#', '#'], ['#', '#']], [['#', '#'], ['#', '#'], ['#', '#']], [['#', '#'], ['#', '#'], ['#', '#']], [['#', '#'], ['#', '#'], ['#', '#']]]
Time Complexity: O(n^3)
Space Complexity: O(n^3)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...