Python – Add custom dimension in Matrix

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 listtest_list =[(5, 6), (1, 2), (7, 8), (9, 12)]# printing original listprint("The original listis: " +str(test_list))# initializing Column values vals =[4, 5, 7, 3]# Add custom dimension in Matrix# Using zip() + list comprehension + "+" operatorres =[i +(j, ) fori, j inzip(test_list, vals)]# printing result print("The result after adding dimension : " +str(res))  | 
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 listtest_list =[(5, 6), (1, 2), (7, 8), (9, 12)]# printing original listprint("The original listis: " +str(test_list))# initializing Column values vals =[4, 5, 7, 3]# Add custom dimension in Matrix# Using zip() + * operatorres =[(*i, j) fori, j inzip(test_list, vals)]# printing result print("The result after adding dimension : " +str(res))  | 
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 listtest_list =[(5, 6), (1, 2), (7, 8), (9, 12)]# printing original listprint("The original list is : "+str(test_list))# initializing Column valuesvals =[4, 5, 7, 3]# Add custom dimension in Matrixres=[]fori inrange(0,len(test_list)):    x=list(test_list[i])    x.append(vals[i])    res.append(tuple(x))# printing resultprint("The result after adding dimension : "+str(res)) | 
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(N)
Auxiliary Space : O(N)
 
				 
					

