Open In App

Python – Add Custom Column to Tuple list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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))


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




# 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))


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




# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using pandas library
 
import pandas as pd
 
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
 
# converting list of tuples to a pandas dataframe
df = pd.DataFrame(test_list, columns=['col1', 'col2'])
 
# initializing add list
cus_eles = [17, 23, 12]
 
# adding a new column to the dataframe
df['new_col'] = cus_eles
 
# converting the dataframe back to a list of tuples
res = [tuple(x) for x in df.to_numpy()]
 
# printing result
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. 



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads