Python – Add Custom Column to Tuple list
Last Updated :
02 May, 2023
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
test_list = [( 3 , 4 ), ( 78 , 76 ), ( 2 , 3 )]
print ("The original list is : " + str (test_list))
cus_eles = [ 17 , 23 , 12 ]
res = [sub + (val, ) for sub, val in zip (test_list, cus_eles)]
print ("The tuples after adding elements : " + str (res))
|
Output :
The original list is : [(3, 4), (78, 76), (2, 3)]
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Time complexity: O(n),
Auxiliary space: O(n)
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
test_list = [( 3 , 4 ), ( 78 , 76 ), ( 2 , 3 )]
print ("The original list is : " + str (test_list))
cus_eles = [ 17 , 23 , 12 ]
res = list ( map ( lambda a, b: a + (b, ), test_list, cur_eles))
print ("The tuples after adding elements : " + str (res))
|
Output :
The original list is : [(3, 4), (78, 76), (2, 3)]
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #3: Add Custom Column to Tuple List using List Comprehension and Zip Function.
Use list comprehension and the built-in zip() function to iterate over both lists in parallel. For each pair of elements (a, b) where a is a tuple from test_list and b is an element from cus_eles, the program creates a new tuple (a[0], a[1], b) where a[0] and a[1] represent the first and second elements of a, respectively, and b is the custom element from cus_eles.
Python3
test_list = [( 3 , 4 ), ( 78 , 76 ), ( 2 , 3 )]
cus_eles = [ 17 , 23 , 12 ]
res = [(a[ 0 ], a[ 1 ], b) for a, b in zip (test_list, cus_eles)]
print ( "The tuples after adding elements : " , res)
|
Output
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Time complexity: O(n), where n is the length of the input list,
Auxiliary space: O(n), as we are creating a new list of the same size as the input list.
Method #4: Using a for loop
Iterates through the index of test_list and concatenates each tuple with the corresponding element in cur_eles. The result is a list of tuples with the added elements.
Python3
test_list = [( 3 , 4 ), ( 78 , 76 ), ( 2 , 3 )]
cur_eles = [ 17 , 23 , 12 ]
res = []
for i in range ( len (test_list)):
res.append(test_list[i] + (cur_eles[i],))
print ( "The tuples after adding elements : " + str (res))
|
Output
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), since the output list res has n elements, and the input lists test_list and cur_eles also have n elements.
Method #5: using the pandas library.
Python3
import pandas as pd
test_list = [( 3 , 4 ), ( 78 , 76 ), ( 2 , 3 )]
df = pd.DataFrame(test_list, columns = [ 'col1' , 'col2' ])
cus_eles = [ 17 , 23 , 12 ]
df[ 'new_col' ] = cus_eles
res = [ tuple (x) for x in df.to_numpy()]
print ( "The tuples after adding elements : " + str (res))
|
Output:
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), where n is the number of tuples in the list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...