Add custom borders to matrix in Python
Last Updated :
15 May, 2023
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)
OutputThe 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)
OutputThe 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))
OutputThe 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.
Similar Reads
Add Two Matrices - Python
The task of adding two matrices in Python involves combining corresponding elements from two given matrices to produce a new matrix. Each element in the resulting matrix is obtained by adding the values at the same position in the input matrices. For example, if two 2x2 matrices are given as:Two 2x2
3 min read
Summation Matrix columns - Python
The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13]. Using numpy.sum()numpy.sum() is a hi
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then
5 min read
Python Program to Construct n*m Matrix from List
We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension
3 min read
Python | Row lengths in Matrix
The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the lengths of rows of matrix in uneven sized matrix. Let's discuss certain ways in which this problem can be solved. Method #1 : Using max() + m
4 min read
Python - Remove front column from Matrix
Sometimes, while working with Matrix data, we can have stray element that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination o
6 min read
Python - Matrix creation of n*n
Matrices are fundamental structures in programming and are widely used in various domains including mathematics, machine learning, image processing, and simulations. Creating an nÃn matrix efficiently is an important step for these applications. This article will explore multiple ways to create such
3 min read
Python - Convert List to custom overlapping nested list
Given a list, the task is to write a Python program to convert it into a custom overlapping nested list based on element size and overlap step. Examples: Input: test_list = [3, 5, 6, 7, 3, 9, 1, 10], step, size = 2, 4Output: [[3, 5, 6, 7], [6, 7, 3, 9], [3, 9, 1, 10], [1, 10]]Explanation: Rows slice
3 min read
Python Program to Print matrix in antispiral form
Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12
2 min read
Python program to add two matrices
Prerequisite : Arrays in Python, Loops, List Comprehension Program to compute the sum of two matrices and then print it in Python. We can perform matrix addition in various ways in Python. Here are a two of them. Examples:Input : X= [[1,2,3], [4 ,5,6], [7 ,8,9]] Y = [[9,8,7], [6,5,4], [3,2,1]] Outpu
2 min read