Multi-Dimensional List In Python


What is List in Python

List is a mutable and sequenced container. It can contain homogeneous objects as well as heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries. It can be indexed and sliced. A list that has other lists as its element is called the Nested list.

Multi-Dimensional List

Multi-dimensional list

A one-dimensional list is a simple list without any sublists.  A two-dimensional list is a list that solely contains many single-dimensional lists. A three-dimensional list solely contains many two-dimensional lists that solely contains many single-dimensional lists. This concludes that Multi-dimensional lists are the lists within the lists within the lists within the lists and so on.

N_Multi-Dimensional_list = [[[[[list of 1D elements],
                               [list of 1D elements]...
                                list of 2D containing 1D lists],
                              [[list of 1D elements], 
                               [list of 1D elements]... 
                                list of 2D containing 1D lists]]...
                                list of 3D containing 2D lists]]],
                             [[[list of 1D elements], 
                               [list of 1D elements]... 
                                list of 2D containing 1D lists], 
                              [[list of 1D elements],
                               [list of 1D elements]... 
                                list of 2D containing 1D lists]]... 
                                list of 3D containing 2D lists]]]...
                                .......
                                list of (N - 1)D containing (N - 2) lists]]]]...
                                list of ND containing (N - 1) lists]]]]]]

The one-dimensional list must be a list however it can be an empty list. The numbers of elements in the lists have nothing to do with which dimension the list is. The multi-dimensional lists are mostly used to represent tables, shapes, matrices, etc or anything that concerns digits in multi-dimensional data in Python. A list can be of infinite dimensions in Python however the mainly used multi-dimensional lists are the two-dimensional list and three-dimensional list.

#One-Dimensional List
One_Dimensional_List = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#Two-Dimensional List
Two_Dimensional_List1 = [[1, 2, 3],
                         [4, 5, 6],
                         [7, 8, 9]] # 3 X 3 Matrix
Two_Dimensional_List2 = [[1, 2, 3, 4],
                         [],
                         [5],
                         [6, 7, 8, 9]]

#Three-Dimensional List
Three_Dimensional_List = [[[1, 2, 3]],
                          [[4, 5, 6],
                           [7, 8, 9]],
                          [[], 
                           [],
                           []]]

#Four-Dimensional List
Four_Dimensional_List = [[[[1, 2, 3],
                           [4, 5, 6],
                           [7, 8, 9]]],
                         [[[1, 2, 3], 
                           [4, 5, 6],
                           [7, 8, 9]]],
                         [[[1, 2, 3],
                           [4, 5, 6],
                           [7, 8, 9]]],
                         [[[1, 2, 3],
                           [4, 5, 6], 
                           [7, 8, 9]]]]

Creating a Multi-Dimensional list using list comprehension

List comprehension method defines a list and its contents at the same time using loops. The basic syntax of list comprehension consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element.

#Empty 3D list
Empty_3D _list = [[[] for i in range(3)] for i in range(3)]
#Output: [[[], [], []], 
          [[], [], []], 
          [[], [], []]]

# m X n matrix of zeroes
m = 3
n = 2
mXn_matrix_of_Zeroes = [[0 for x in range(n)] for x in range(m)] 
print(mXn_matrix_of_Zeroes)
#Output: [[0, 0],
          [0, 0],
          [0, 0]]

#4D list containing same 2D continous list
_4D_list = [[[[i,(i+1),(i+2)] for i in range(1,10,3)]] for x in range(3)]
print(_4D_list)
#Output: [[[[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]],
          [[[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]],
          [[[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]]]

#Continous Multi-Dimensional list
Continous_Multi-Dimensional_list = [[[i,(i+1),(i+2)] for i in range(1*x,9+x,3)] for x in range(1,28,9)] #3D list comprehension
print(Continous_Multi-Dimensional_list)
#Output: [[[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]],
          [[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]],
          [[19, 20, 21],
           [22, 23, 24],
           [25, 26, 27]]]

#Multi-Dimensional list of random integers
Multi_Dimensional_list_of_random_integers = [[[[[random.randint(1, 30) for _ in range(4)]]] for x in range(5)]]
print(Multi_Dimensional_list_of_random_integers)
#Output: [[[[[2, 18, 15, 17]]], 
          [[[15, 1, 7, 4]]],
          [[[18, 26, 1, 25]]],
          [[[10, 26, 21, 13]]],
          [[[14, 27, 28, 27]]]]]

Accessing elements of  Multi-Dimensional list

Python for loop statement iterates over iterables that are objects which return its element one by one. Sequences are ordered sets of iterables that have specific set of features. Lists, tuples, and strings are all sequences. Multidimensional lists behave just like regular single-dimensional lists.

#2-Dimensional list
List_2D = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]]
for list in List_2D:
    print(list)
#Output: [1, 2, 3]
         [4, 5, 6]
         [7, 8, 9]

#3-Dimensional list
List_3D = [[[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]],
          [[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]],
          [[19, 20, 21],
           [22, 23, 24],
           [25, 26, 27]]] 
for list in List_3D:
    print(list)
#Output:  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
          [[10, 11, 12], [13, 14, 15], [16, 17, 18]]
          [[19, 20, 21], [22, 23, 24], [25, 26, 27]]

#By using nested loops, sub-lists can be accessed.

#2-Dimensional list
for list in list_2D: 
    for ele in list: 
        print(ele)
#Output: 1
         2
         3
         4
         5
         6
         7
         8
         9

#3-Dimensional list
for list_2D in List_3D:
    for list in list_2D:
        for ele in list:
            print(ele)
#Output: 1
         2
         3
         4
         5
         6
         7
         8
         9
         10
         11
         12
         13
         14
         15
         16
         17
         18
         19
         20
         21
         22
         23
         24
         25
         26
         27

In python, every element of an ordered sequence is accessed based on their position in the sequence by using square bracket notation[] to index them. To access a list with nested lists, multiple brackets are used. Each bracket corresponds to each dimension of the list. The first bracket corresponds to the outer dimensional list and the second index corresponds to the next dimensional list and so on.

 

Indexing Multi-Dimensional list

#Accessing each element of multi-dimensional list with indexing
List_2D = [[1, 2, 3],  
           [4, 5, 6],
           [7, 8, 9, 10]]  #2-Dimensional list
for i in range(0, len(List_2D)):
    for j in range(0, len(List_2D[i])):
        print(f"[{i}][{j}] = {List_2D[i][j]}")
#Output: [0][0] = 1
         [0][1] = 2
         [0][2] = 3
         [1][0] = 4
         [1][1] = 5
         [1][2] = 6
         [2][0] = 7
         [2][1] = 8
         [2][2] = 9
         [2][3] = 10

#Accessing an element at specific position of multi-dimensional list with indexing
List_3D = [[[1, 2, 3],
            []],
           [[]],
           [[7, 8, 9],
            [10, 11, 12],
            [13, 14, 15]],
           [[22, 23, 24]]] #3-Dimensional list
print(List_3D[2])
#Output: [[7, 8, 9],
          [10, 11, 12],
          [13, 14, 15]]

print(List_3D[1]) 
#Output: [[]]

#To access elements of lower lists more square brackets are added to the indexing notation
print(List_3D[0][0]) 
#Output: [1, 2, 3]

print(List_3D[-1][0]) 
#Output: [22, 23, 24]

print(List_3D[2][1]) 
#Output: [10, 11, 12]

print(List_3D[1][0]) 
#Output: []

print(List_3D[2][2][1]) 
#Output: 14
 
print(List_3D[0][1][0]) 
#Output: IndexError: list index out of range

Modifying elements of Multi-Dimensional list

A list is mutable in python, thus its elements can be updated, overloaded, removed, added, appended, extended etc. Modifying an element of a Multi-Dimensional list is just like modifying an element of a normal list.

List_2D = [[1, 2, 3, 4],
           [],
           [5],
           [6, 7, 8, 9]] #2-Dimensional list

#adding a sublist 
List_2D.append([10, 11]) #append method zips all the elements passed as a single element and adds this element to the end of a list, the length of the list will increase by one
print(List_2D)
#Output: [[1, 2, 3, 4],
          [],
          [5],
          [6, 7, 8, 9],
          [10, 11]]

List_2D.insert(len(List_2D),[12, 13]) #insert method adds an element to the list at the specified index
#Output: [[1, 2, 3, 4],
          [],
          [5],
          [6, 7, 8, 9],
          [10, 11],
          [12, 13]]

#adding multiple sublist
List_2D.extend([[14, 15, 16], [17, 18]]]) #extend method adds each element of the iterable passed as an individual element to the end of the list, the length of the list increases by the number of elements in the iterable.
print(List_2D)
#Output: [[1, 2, 3, 4],
          [],
          [5],
          [6, 7, 8, 9],
          [10, 11],
          [12, 13],
          [14, 15, 16],
          [17, 18]]

#adding an element at the end of a sublist 
List_2D[-1].append(19) 
print(List_2D)
#Output: [[1, 2, 3, 4],
          [],  
          [5],
          [6, 7, 8, 9],
          [10, 11],
          [12, 13],
          [14, 15, 16],
          [17, 18, 19]]

#adding multiple elements at the end of a sublist
List_2D[1].extend([0, 20]) 
print(List_2D)
#Output: [[1, 2, 3, 4],
          [0, 20],
          [5],
          [6, 7, 8, 9],
          [10, 11],
          [12, 13],
          [14, 15, 16],
          [17, 18, 19]]

#adding an element to a sublist at the specified index
List_2D[2].insert(0, 25) 
print(List_2D)
#Output: [[1, 2, 3, 4],
          [0, 20],
          [25, 5],
          [6, 7, 8, 9],
          [10, 11],
          [12, 13],
          [14, 15, 16],
          [17, 18, 19]]

#removing a sublist from the end of the list
List_2D.pop()
print(List_2D)
#Output: [[1, 2, 3, 4],
          [0, 20], 
          [25, 5],
          [6, 7, 8, 9],
          [10, 11], 
          [12, 13], 
          [14, 15, 16]] 

#removing a sublist from a specific position in the list
List_2D.pop(3)
print(List_2D) 
#Output: [[1, 2, 3, 4],
          [0, 20],
          [25, 5],
          [10, 11],
          [12, 13],
          [14, 15, 16]]

#removing a specific sublist from the list
List_2D.remove([0,20])
print(List_2D)
#Output: [[1, 2, 3, 4],
          [25, 5],
          [10, 11],
          [12, 13],
          [14, 15, 16]]

#removing an element from the end of the sublist 
List_2D[-1].pop()
print(List_2D) 
#Output: [[1, 2, 3, 4],
          [25, 5],
          [10, 11],
          [12, 13],
          [14, 15]]

#removing an element from a specific position in the sublist
List_2D[0].pop(2)
print(List_2D) 
#Output: [[1, 2, 4],
          [25, 5],
          [10, 11],
          [12, 13],
          [14, 15]]

#removing a specific element from the sublist
List_2D[2].remove(11)
print(List_2D) 
#Output: [[1, 2, 4],
          [25, 5],
          [10],
          [12, 13],
          [14, 15]]

#emptying a sublist
List_2D[-2].clear()
print(List_2D)
#Output: [[1, 2, 4],
          [25, 5],
          [10],
          [],
          [14, 15]]

#overding a sublist
List_2D[2] = [12, 6, 9]
print(List_2D) 
#Output: [[1, 2, 4],
          [25, 5],
          [12, 6, 9],
          [],
          [14, 15]]

#overding an element in a sublist
List_2D[0][-1] = 3
print(List_2D) 
#Output: [[1, 2, 3],
          [25, 5],
          [12, 6, 9],
          [],
          [14, 15]]

#reversing a multi-dimensional list
List_2D.reverse()
print(List_2D)
#Output: [[14, 15],
          [],
          [12, 6, 9],
          [25, 5],
          [1, 2, 3]]

#reversing a sublist
List_2D[3].reverse()
print(List_2D) 
#Output: [[14, 15],
          [],
          [12, 6, 9],
          [5, 25],
          [1, 2, 3]]

#sorting a multi-dimensional list
List_2D.sort()
print(List_2D)
#Output: [[],
          [1, 2, 3],
          [5, 25],
          [12, 6, 9],
          [14, 15]]

#sorting a sublist 
List_2D[-2].sort() 
print(List_2D) 
#Output: [[],
          [1, 2, 3],
          [5, 25],
          [6, 9, 12],
          [14, 15]]

z

Leave a Comment

%d bloggers like this: