Add custom borders to matrix in Python

• Last Updated : 10 Oct, 2022

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 + 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 |```

My Personal Notes arrow_drop_up