Sometimes, while working with Python records, we can have a problem in which we need to add custom column to tuples list. This kind of problem can have application in data domains such as web development. Lets discuss certain ways in which this task can be performed.
Input :
test_list = [(3, ), (7, ), (2, )]
cus_eles = [7, 8, 2]
Output : [(3, 7), (7, 8), (2, 2)]Input :
test_list = [(3, 9, 6, 10)]
cus_eles = [7]
Output : [(3, 9, 6, 10, 7)]
Method #1 : Using list comprehension + zip()
The combination of above functionalities can be used to solve this problem. In this, we perform the pairing of custom elements and tuples with the help of zip().
# Python3 code to demonstrate working of # Add Custom Column to Tuple list # Using list comprehension + zip() # initializing list test_list = [(3, 4), (78, 76), (2, 3)] # printing original list print("The original list is : " + str(test_list)) # initializing add list cus_eles = [17, 23, 12] # Add Custom Column to Tuple list # Using list comprehension + zip() res = [sub + (val, ) for sub, val in zip(test_list, cus_eles)] # printing result print("The tuples after adding elements : " + str(res)) |
The original list is : [(3, 4), (78, 76), (2, 3)] The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Method #2 : Using map() + lambda
The combination of above functions can also be used to solve this problem. In this, we perform the task of extending logic to each tuple using map() and lambda is used to perform task of addition.
# Python3 code to demonstrate working of # Add Custom Column to Tuple list # Using map() + lambda # initializing list test_list = [(3, 4), (78, 76), (2, 3)] # printing original list print("The original list is : " + str(test_list)) # initializing add list cus_eles = [17, 23, 12] # Add Custom Column to Tuple list # Using map() + lambda res = list(map(lambda a, b: a +(b, ), test_list, cur_eles)) # printing result print("The tuples after adding elements : " + str(res)) |
The original list is : [(3, 4), (78, 76), (2, 3)] The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Recommended Posts:
- Python - Add K to Minimum element in Column Tuple List
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Add custom values key in List of dictionaries
- Python | Summation of Kth Column of Tuple List
- Python - Get maximum of Nth column from tuple list
- Python - Kth Column Product in Tuple List
- Python - Column Minimum in Tuple list
- Python | Column Mean in tuple list
- Python - Custom Tuple Key Summation in Dictionary
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Convert Matrix to Custom Tuple Matrix
- Python - Custom Rows Removal depending on Kth Column
- Python | Add tuple to front of list
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Add custom dimension in Matrix
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.

