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 : [[['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#']], [['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#']]]
# Python program to print 3D list # importing pretty printed 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 # Driver Code col1 = 5col2 = 3row = 2# used the pretty printed function pprint.pprint(ThreeD(col1, col2, row)) |
chevron_right
filter_none
Output:
[[['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#']], [['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#']]]
Refer pprint() to get more insight into this topic.
Now let’s suppose we need to merge two 3D lists into one.
# Python program to merge two 3D list into one # importing pretty printed 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)] # Merging using "+" operator lst = lst1+lst2 return lst # Driver Code col1 = 3col2 = 3row = 3 # used the pretty printed function pprint.pprint(ThreeD(col1, col2, row)) |
chevron_right
filter_none
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']]]
Recommended Posts:
- Creating a sorted merged list of two unsorted lists in Python
- Creating a Pandas dataframe using list of tuples
- Creating Pandas dataframe using list of lists
- Creating Series from list, dictionary, and numpy array in Pandas
- Creating linear kernel SVM in Python
- Python | Creating a button in tkinter
- Python | Creating Multidimensional dictionary
- Creating a Proxy Webserver in Python | Set 2
- Python | Catching and Creating Exceptions
- Creating a Proxy Webserver in Python | Set 1
- Creating NotePad using PyQt5 - Python
- Creating child process using fork() in Python
- Python | Creating a Simple Drawing App in kivy
- Creating Tabbed Widget With Python-Tkinter
- Python | Creating tensors using different functions in Tensorflow
- Creating a Basic hardcoded ChatBot using Python-NLTK
- Creating Python Virtual Environment in Windows and Linux
- Python | Creating DataFrame from dict of narray/lists
- Python | Creating a Pandas dataframe column based on a given condition
- Python | Convert list of string to list of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

