Python - Add custom dimension in Matrix
Last Updated :
14 Mar, 2023
Sometimes, while working with Python Matrix, we can have a problem in which we need to add another dimension of custom values, this kind of problem can have problem in all kinds of domains such as day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(5, 6, 7, 8)] vals = [10] Output : [(5, 6, 7, 8, 10)] Input : test_list = [(5, ), (6, ), (7, ), (8, )] vals = [10, 9, 8, 7] Output : [(5, 10), (6, 9), (7, 8), (8, 7)]
Method #1 : Using zip() + list comprehension + "+" operator The combination of above functions can be used to solve this problem. In this, we use + operator to add an element and zip() is used to extend this logic to every row of Matrix.
Python3
# Python3 code to demonstrate working of
# Add custom dimension in Matrix
# Using zip() + list comprehension + "+" operator
# initializing list
test_list = [(5, 6), (1, 2), (7, 8), (9, 12)]
# printing original list
print("The original list is : " + str(test_list))
# initializing Column values
vals = [4, 5, 7, 3]
# Add custom dimension in Matrix
# Using zip() + list comprehension + "+" operator
res = [i + (j, ) for i, j in zip(test_list, vals)]
# printing result
print("The result after adding dimension : " + str(res))
Output : The original list is : [(5, 6), (1, 2), (7, 8), (9, 12)]
The result after adding dimension : [(5, 6, 4), (1, 2, 5), (7, 8, 7), (9, 12, 3)]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Method #2 : Using zip() + * operator The combination of above functions can be used to solve this problem. In this, we perform the task of joining using unpacking operator to unpack values and then perform the join of custom values.
Python3
# Python3 code to demonstrate working of
# Add custom dimension in Matrix
# Using zip() + * operator
# initializing list
test_list = [(5, 6), (1, 2), (7, 8), (9, 12)]
# printing original list
print("The original list is : " + str(test_list))
# initializing Column values
vals = [4, 5, 7, 3]
# Add custom dimension in Matrix
# Using zip() + * operator
res = [(*i, j) for i, j in zip(test_list, vals)]
# printing result
print("The result after adding dimension : " + str(res))
Output : The original list is : [(5, 6), (1, 2), (7, 8), (9, 12)]
The result after adding dimension : [(5, 6, 4), (1, 2, 5), (7, 8, 7), (9, 12, 3)]
Method #3 : Using for loop, list(),tuple() methods
Approach
- Initiate a for loop
- Convert each tuple of test_list to list and then append each element of val to e according to index
- After append convert list to tuple again and append that tuple to output list
- Display output list
Python3
# Python3 code to demonstrate working of
# Add custom dimension in Matrix
# initializing list
test_list = [(5, 6), (1, 2), (7, 8), (9, 12)]
# printing original list
print("The original list is : " + str(test_list))
# initializing Column values
vals = [4, 5, 7, 3]
# Add custom dimension in Matrix
res=[]
for i in range(0,len(test_list)):
x=list(test_list[i])
x.append(vals[i])
res.append(tuple(x))
# printing result
print("The result after adding dimension : " + str(res))
OutputThe original list is : [(5, 6), (1, 2), (7, 8), (9, 12)]
The result after adding dimension : [(5, 6, 4), (1, 2, 5), (7, 8, 7), (9, 12, 3)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Python | Custom length Matrix
Sometimes, we need to initialize a matrix in Python of variable length from the list containing elements. In this article, we will discuss the variable length method initialization and certain shorthands to do so. Let's discuss certain ways to perform this. Method #1: Using zip() + list comprehensio
6 min read
Add custom borders to matrix in Python
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 = [[
5 min read
Python | Encoding Decoding using Matrix
This article is about how we use the matrix to encode and decode a text message and simple strings. Encoding process : Take a String convert to corresponding number shown below convert to 2D matrix(array). Now we have 2x2 matrix! When we multiply this matrix with encoding matrix we get encoded 2x2 m
4 min read
Type Casting Whole List and Matrix - Python
The task of type casting a whole list and matrix in Python involves converting all elements of a list or a matrix to a specific data type while preserving the structure. Given a list or matrix of integers, the goal is to transform each element into another data type, such as a string or float, effic
3 min read
Python - Convert Matrix to Dictionary
The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read
Python - Row-wise element Addition in Tuple Matrix
Sometimes, while working with Python tuples, we can have a problem in which we need to perform Row-wise custom elements addition in Tuple matrix. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed.Input : test_list = [[('Gfg', 3)
4 min read
How to Create a Sparse Matrix in Python
If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are: Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.Computing time:
3 min read
Python - K Matrix Initialization
Sometimes in the world of competitive programming, we need to initialise the matrix, but we donât wish to do it in a longer way using a loop. We need a shorthand for this. This type of problem is quite common in dynamic programming domain. Letâs discuss certain ways in which this can be done. Method
5 min read
Initialize Matrix in Python
There are many ways to declare a 2 dimensional array with given number of rows and columns. Let us look at some of them and also at the small but tricky catches that accompany it. We can do it using list comprehension, concatenation feature of * operator and few other ways. Method 0: 2 list comprehe
4 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