In this Python article we are going to talk about How to Create Multidimensional Lists in Python, Python is popular programming language that is widely used for different purposes. it is known for its simplicity and easy of use and this makes it an excellent choice for beginners as well as experts. one of the useful features of Python is the ability to create multidimensional lists. In this article we are going to discuss how to create multidimensional lists in Python.
Multidimensional list is a list of lists where each element in the list is another list. number of lists that make up the multidimensional list depends on the dimensions you want to create. for example two dimensional list is a list of lists, where each element in the list is one dimensional list.
How to Create Multidimensional Lists in Python
For creating multidimensional list in Python we can use nested lists. nested list is a list that is contained within another list. this is an example of how to create two dimensional list in Python:
1 |
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
In the above example we have created two dimensional list that contains three lists, each containing three elements.
for accessing an element in the list, we can use indexing. for example to access the element in the second row and third column of the list, you can use following code:
1 2 |
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(my_list[1][2]) |
This will be the output
You can also create multidimensional lists using loops.
1 |
my_list = [[[0 for k in range(3)] for j in range(3)] for i in range(3)] |
In the above example we have created three dimensional list that contains three lists, each containing three lists, each containing three elements.
for accessing an element in the list, you can use indexing. for example to access the element in second row, third column and first layer of the list, you can use following code:
1 2 |
my_list = [[[0 for k in range(3)] for j in range(3)] for i in range(3)] print(my_list[1][2][0]) |
This is the result
Learn More on Python
- How to Integrate PyQt6 with OpenCV
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6