Python | Modifying tuple contents with list
Last Updated :
22 Mar, 2023
In Python, tuples are immutable and hence no changes are required in them once they are formed. This restriction makes their processing harder and hence certain operations on tuples are quite useful to have knowledge of. This article deals with modifying the second tuple element with the list given. Let’s discuss certain ways in which this can be done.
Method #1: Using for loop
Python3
test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
test_list2 = [ 4 , 5 , 6 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for i in range ( 0 , len (test_list1)):
x = (test_list1[i][ 0 ],test_list2[i])
res.append(x)
print ( "The modified resultant list of tuple : " + str (res))
|
Output
The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The original list 2 : [4, 5, 6]
The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
Time Complexity: O(N), where N is the length of the input lists test_list1 and test_list2.
Auxiliary Space: O(N)
Method #2: Using zip() + list comprehension
In this method, we just take the first element of list of tuple and the element at corresponding index and zip them together using the zip function.
Python3
test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
test_list2 = [ 4 , 5 , 6 ]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
res = [(i[ 0 ], j) for i, j in zip (test_list1, test_list2)]
print ("The modified resultant list of tuple : " + str (res))
|
Output :
The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The original list 2 : [4, 5, 6]
The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
Method #3: Using zip() + map() + operator.itemgetter()
The itemgetter function here does the task of fetching the constant of two tuple elements which is then mapped with the corresponding index using the map function. The zip function is used to extend this logic to the entire list.
Python3
import operator
test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
test_list2 = [ 4 , 5 , 6 ]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
temp = map (operator.itemgetter( 0 ), test_list1)
res = list ( zip (temp, test_list2))
print ("The modified resultant list of tuple : " + str (res))
|
Output :
The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The original list 2 : [4, 5, 6]
The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
Method #4: Using for itertools module:
The itertools.zip_longest() function is used to iterate through two lists test_list1 and test_list2 simultaneously. For each iteration, it creates a tuple (tup1, tup2) where tup1 is an element from test_list1 and tup2 is an element from test_list2.
The resulting list res is created using a list comprehension that iterates through the tuples produced by itertools.zip_longest(). For each iteration, it creates a new tuple with the first element of tup1 and the second element of tup2. The resulting list res contains the modified tuples.
Here is an example using the itertools module:
Python3
import itertools
test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
test_list2 = [ 4 , 5 , 6 ]
print ( "The original list 1:" , test_list1)
print ( "The original list 2:" , test_list2)
res = [(tup1[ 0 ], tup2) for tup1, tup2 in itertools.zip_longest(test_list1, test_list2)]
print ( "The modified resultant list of tuple:" , res)
|
Output
The original list 1: [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The original list 2: [4, 5, 6]
The modified resultant list of tuple: [('Geeks', 4), ('for', 5), ('Geeks', 6)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using enumerate
In this example, the modify_tuples function takes in two arguments: list1, which is the list of tuples to be modified, and list2, which is the list of values to be used for modification. The function loops through each tuple in list1, and uses the enumerate function to get both the index and the values of each tuple. The function then creates a new tuple with the same first value as the original tuple, and the corresponding value from list2. The function appends the new tuple to a result list. Finally, the function returns the result list containing the modified tuples.
Python3
def modify_tuples(list1, list2):
result = []
for i, (x, y) in enumerate (list1):
result.append((x, list2[i]))
return result
list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
list2 = [ 4 , 5 , 6 ]
result = modify_tuples(list1, list2)
print (result)
|
Output
[('Geeks', 4), ('for', 5), ('Geeks', 6)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using map() and lambda function:
Algorithm:
1.Initialize two lists, test_list1 and test_list2.
2.Print the original lists.
3.Use the map() and lambda function to modify the tuple elements.
4.The lambda function extracts the first element from the tuple in test_list1 and combines it with the corresponding 5.element from test_list2 to form a new tuple.
6.Convert the result of the map() function into a list.
7.Print the modified resultant list of tuple.
Python3
test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )]
test_list2 = [ 4 , 5 , 6 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = list ( map ( lambda x, y: (x[ 0 ], y), test_list1, test_list2))
print ( "The modified resultant list of tuple : " + str (res))
|
Output
The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The original list 2 : [4, 5, 6]
The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
Time Complexity:
The time complexity of this algorithm is O(n), where n is the length of the lists test_list1 and test_list2. The map() function has a time complexity of O(n), as it needs to iterate over each element of the lists. The lambda function is a constant time operation, so it does not affect the overall time complexity.
Auxiliary Space:
The space complexity of this algorithm is also O(n), as we are creating a new list to store the modified tuples. The size of the new list will be equal to the length of the input lists, so the space complexity is linear with respect to the input size.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...