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.
Similar Reads
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
6 min read
Python | Convert Lists to column tuples
Sometimes, while working with data, we can have a problem in which we need to get alike index elements in a single container. This means the columns of Matrix/Multiple lists need to be converted to list of tuples, each comprising of like index elements. Let's discuss certain ways in which this task
11 min read
Python | Add tuple to front of list
Sometimes, while working with Python list, we can have a problem in which we need to add a new tuple to existing list. Append at rear is usually easier than addition at front. Let's discuss certain ways in which this task can be performed. Method #1 : Using insert() This is one of the way in which t
7 min read
Python - Add K to Minimum element in Column Tuple List
Sometimes, while working with Tuple records, we can have a problem in which we need to perform task of adding certain element to max/ min element to each column of Tuple list. This kind of problem can have application in web development domain. Let's discuss a certain way in which this task can be p
8 min read
Convert List to Tuple in Python
The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Python - Convert Matrix to Custom Tuple Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform conversion of a Python Matrix to matrix of tuples which a value attached row-wise custom from external list. This kind of problem can have applications in data domains as Matrix is integral DS that is used
6 min read
Python | Column Mean in tuple list
Sometimes, while working with records, we can have a problem in which we need to average all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using sum() + l
4 min read
Python | Adding N to Kth tuple element
Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let's discuss certain ways in which N can be added to Kth element of tuple in list. Method #1 : Using loop Using loops this task c
3 min read
Convert Dictionary to List of Tuples - Python
Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read
Python List and Tuple Combination Programs
Lists and tuples are two of the most commonly used data structures in Python. While lists are mutable and allow modifications, tuples are immutable and provide a stable structure for storing data. This article explores various programs related to list and tuple combinations, covering topics like: So
6 min read