There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional list in Python.
Approach 1:
# Python program to demonstrate printing # of complete multidimensional list a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]] print(a) |
[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
Approach 2: Accessing with the help of loop.
# Python program to demonstrate printing # of complete multidimensional list row # by row. a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]] for record in a: print(record) |
[2, 4, 6, 8, 10] [3, 6, 9, 12, 15] [4, 8, 12, 16, 20]
Approach 3: Accessing using square brackets.
Example:
# Python program to demonstrate that we # can access multidimensional list using # square brackets a = [ [2, 4, 6, 8 ], [ 1, 3, 5, 7 ], [ 8, 6, 4, 2 ], [ 7, 5, 3, 1 ] ] for i in range(len(a)) : for j in range(len(a[i])) : print(a[i][j], end=" ") print() |
2 4 6 8 1 3 5 7 8 6 4 2 7 5 3 1
# Python program to create a m x n matrix # with all 0s m = 4n = 5 a = [[0 for x in range(n)] for x in range(m)] print(a) |
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
1. append(): Adds an element at the end of the list.
Example:
# Adding a sublist a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]] a.append([5, 10, 15, 20, 25]) print(a) |
[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [5, 10, 15, 20, 25]]
2. extend(): Add the elements of a list (or any iterable), to the end of the current list.
# Extending a sublist a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]] a[0].extend([12, 14, 16, 18]) print(a) |
[[2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
3. reverse(): Reverses the order of the list.
# Reversing a sublist a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]] a[2].reverse() print(a) |
[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [20, 16, 12, 8, 4]]
Recommended Posts:
- Convert Python Nested Lists to Multidimensional NumPy Arrays
- Multidimensional data analysis in Python
- Python | Creating Multidimensional dictionary
- Multidimensional Arrays in Java
- Multidimensional arrays in PHP
- PHP multidimensional array search by value
- How to search by key=>value in a multidimensional array in PHP ?
- Sort a multidimensional array by date element in PHP
- Perl | Multidimensional Hashes
- How to check an array is multidimensional or not in PHP ?
- Perl | Multidimensional Arrays
- How to merge the duplicate value in multidimensional array in PHP ?
- Multidimensional array in JavaScript
- Multidimensional Associative Array in PHP
- Multidimensional Array in R
- Evaluate Einstein's summation convention of two multidimensional NumPy arrays
- How to access different rows of a multidimensional NumPy array?
- Python | Zipping two lists of lists
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
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.


