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 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)) |
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 #2 : Using zip() + * operator
The combination of above functions can be used to solve this problem. In this, we perform the task of joinig using unpacking operator to unpack values and then perform the join of custom values.
# 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)) |
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)]
Recommended Posts:
- Python - Adjacent Coordinates in N dimension
- Change the dimension of a NumPy array
- Calculate the mean across dimension in a 2D NumPy array
- Python - Convert Matrix to Custom Tuple Matrix
- Python - Add Custom Column to Tuple list
- Python - Add custom values key in List of dictionaries
- Python | Custom length Matrix
- Python - Matrix Custom Multiplier
- Python - Custom Columns Matrix
- Python | User groups with Custom permissions in Django
- Python | Custom list split
- Python | Custom Multiplication in list of lists
- Python | Custom Cycle list
- Python | Custom slicing in List
- Python | Custom sorting in list of tuples
- Python - Initialize dictionary with custom value list
- Python - Custom dictionary initialization in list
- Python | Custom List slicing Sum
- Python | Custom Index Range Summation
- Python | Consecutive Custom Chunked elements Product
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

