Home »
Python »
Python Articles
Python - Multidimensional Lists
By IncludeHelp Last updated : February 17, 2024
A multidimensional list refers to a Python list having more than one dimension. Python allows the creation of multidimensional lists by using lists inside a list.
An example of a multidimensional list is:
[[10, 20, 30, 40, 50], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
Creating multidimensional list
To create a multidimensional list, you can write lists inside a list separated by commas.
Example
The below example creates a multidimensional list:
# Creating multidimensional list
list1 = [[111, 222, 333], [444, 555, 666], [777, 888, 999]]
# Printing
print("Multidimensional List:\n", list1)
The output of the above example is:
Multidimensional List:
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
Creating multidimensional list with all zeros
To create a multidimensional list with all zeros, you can simply assign zero (0) as all elements using the for loop and the range() method.
Example
The below example creates a multidimensional list with all zeros:
# Creating multidimensional list with all zeros
row = 3 # total sublists
col = 4 # elements in a sublist
list1 = [[0 for x in range(col)] for x in range(row)]
# Printing
print("Multidimensional List:\n", list1)
The output of the above example is:
Multidimensional List:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Accessing elements of multidimensional list
You can use the nested for loop and indexing (just like C arrays) to access the elements of a multidimensional list in Python.
Example
The below example accesses the elements of a multidimensional list:
# Creating multidimensional list
list1 = [[111, 222, 333], [444, 555, 666], [777, 888, 999]]
# Printing multidimensional list
print("Multidimensional List:\n", list1)
# Accessing elements of multidimensional list
print("Elements of multidimensional list:")
for i in range(len(list1)):
for j in range(len(list1[i])):
print(list1[i][j], end=" ")
print()
The output of the above example is:
Multidimensional List:
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
Elements of multidimensional list:
111 222 333
444 555 666
777 888 999
Appending a list to multidimensional list
You can append a list to a multidimensional list by using the append() method. The append() method accepts a list as an argument and appends it to the current list.
Example
The below example appends a list to multidimensional list:
# Creating multidimensional list
list1 = [[111, 222, 333], [444, 555, 666], [777, 888, 999]]
# Printing multidimensional list
print("Multidimensional List Before Appending:\n", list1)
# Appending list to multidimensional list
list1.append([123, 456, 789])
# Printing multidimensional list
print("Multidimensional List After Appending:\n", list1)
The output of the above example is:
Multidimensional List Before Appending:
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
Multidimensional List After Appending:
[[111, 222, 333], [444, 555, 666], [777, 888, 999], [123, 456, 789]]
Extending multidimensional list
You can extend a multidimensional list using the extend() method by passing a list (or any iterable) as elements in multidimensional lists.
Example
The below example extends a multidimensional list:
# Creating multidimensional list
list1 = [[111, 222, 333], [444, 555, 666], [777, 888, 999]]
# Printing multidimensional list
print("Multidimensional List Before Extending:\n", list1)
# Extending list to multidimensional list
list1.extend([[123, 456, 789], [10, 20, 30]])
# Printing multidimensional list
print("Multidimensional List After Extending:\n", list1)
The output of the above example is:
Multidimensional List Before Extending:
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
Multidimensional List After Extending:
[[111, 222, 333], [444, 555, 666], [777, 888, 999], [123, 456, 789], [10, 20, 30]]
Reversing multidimensional list
You can use the reverse() method to reverse the order of the sub-lists in a multidimensional list.
Example
The below example reverses the order of the sub-lists in multidimensional list:
# Creating multidimensional list
list1 = [[111, 222, 333], [444, 555, 666], [777, 888, 999]]
# Printing multidimensional list
print("Multidimensional List Before Reversing:\n", list1)
# Reversing list to multidimensional list
list1.reverse()
# Printing multidimensional list
print("Multidimensional List After Reversing:\n", list1)
The output of the above example is:
Multidimensional List Before Reversing:
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
Multidimensional List After Reversing:
[[777, 888, 999], [444, 555, 666], [111, 222, 333]]
To understand the above examples, you should have the basic knowledge of the following Python topics: