Open In App

Add custom borders to matrix in Python

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, the task is to write a python program to print each row having custom borders.

Input : test_list = [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3 ,1]], bord = "|"
Output : | 4 5 6 |
         | 1 4 5 |
         | 6 9 1 |
         | 0 3 1 |
Explanation : Matrix is ended using | border as required.
Input : test_list = [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3 ,1]], bord = "!"
Output : ! 4 5 6 !
         ! 1 4 5 !
         ! 6 9 1 !
         ! 0 3 1 !
Explanation : Matrix is ended using ! border as required.

Method 1: Using loop

In this, we perform the task of printing the row elements using the inner loop, separated by space. The main step of adding custom borders is concatenated using the + operator.

Python3




# Python3 code to demonstrate working of
# Custom Matrix Borders
# Using loop
 
# initializing list
test_list = [[4, 5, 6], [1, 4, 5],
             [6, 9, 1], [0, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing border
bord = "|"
 
for sub in test_list:
    temp = bord + " "
 
    # inner row
    for ele in sub:
        temp = temp + str(ele) + " "
 
    # adding border
    temp = temp + bord
    print(temp)


Output

The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |

Time Complexity: O(n2)
Auxiliary Space: O(1)

Method #2 : Using * operator + loop

In this, the task of joining inner characters is performed using * operator. 

Python3




# Python3 code to demonstrate working of
# Custom Matrix Borders
# Using * operator + loop
 
# initializing list
test_list = [[4, 5, 6], [1, 4, 5],
             [6, 9, 1], [0, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing border
bord = "|"
 
for sub in test_list:
 
    # * operator performs task of joining
    print(bord, *sub, bord)


Output

The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #3: Using lists and its methods(insert(),append())

Python3




# Python3 code to demonstrate working of
# Custom Matrix Borders
 
# initializing list
test_list = [[4, 5, 6], [1, 4, 5],
             [6, 9, 1], [0, 3, 1]]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing border
bord = "|"
res = []
 
for i in test_list:
   
    x = list(map(str, i))
    x.insert(0, bord)
    x.append(bord)
     
    print(" ".join(x))


Output

The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using list comprehension

Step-by-step approach:

  • Initialize the border character board.
  • Define a list comprehension that iterates over each sublist in test_list and concatenates the border character bord to the beginning and end of each sublist.
  • Store the result in a new list res.
  • Print the elements of res using a loop.

Python3




# Python3 code to demonstrate working of
# Custom Matrix Borders
 
# initializing list
test_list = [[4, 5, 6], [1, 4, 5],
            [6, 9, 1], [0, 3, 1]]
 
# initializing border
bord = "|"
 
# Using list comprehension
res = [bord + " ".join(map(str, lst)) + bord for lst in test_list]
 
# printing result
for r in res:
    print(r)


Output

|4 5 6|
|1 4 5|
|6 9 1|
|0 3 1|

Time complexity: O(nm), where n is the number of sublists in test_list and m is the maximum length of a sublist.
Auxiliary space: O(nm), where n is the number of sublists in test_list and m is the maximum length of a sublist.

Method 5: Using the string concatenation and multiplication operations.

Step-by-step approach:

  • Iterate through each row of the matrix using a for loop.
  • Concatenate the border character “|” to the beginning and end of the row using string concatenation operation.
  • Use a nested for loop to iterate through each element of the row.
  • Convert the element to a string using the str() function and concatenate a space character ” ” to it.
  • Multiply the resulting string by the number of columns in the matrix minus 2 to fill the space between the borders.
  • Concatenate the resulting string to the row using string concatenation operation.
  • Append the resulting row to the list of results.
  • Print the results.

Python3




# Python3 code to demonstrate working of
# Custom Matrix Borders
 
# initializing list
test_list = [[4, 5, 6], [1, 4, 5],
            [6, 9, 1], [0, 3, 1]]
 
# initializing border
bord = "|"
 
# Using string concatenation and multiplication operations
rows = len(test_list)
cols = len(test_list[0])
res = []
for row in test_list:
    row_str = bord
    for elem in row:
        row_str += str(elem) + " " * (cols - 2) + bord
    res.append(row_str)
     
# printing result
for r in res:
    print(r)


Output

|4 |5 |6 |
|1 |4 |5 |
|6 |9 |1 |
|0 |3 |1 |

Time complexity: O(rows * cols), where rows and cols are the number of rows and columns in the matrix, respectively. We iterate through each element of the matrix once to construct the resulting strings.
Auxiliary space: O(rows), where rows is the number of rows in the matrix. We store the resulting strings in a list of length equal to the number of rows in the matrix.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads