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
# Python3 code to demonstrate # modifying tuple elements # initializing lists test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] test_list2 = [ 4 , 5 , 6 ] # printing original lists print ( "The original list 1 : " + str (test_list1)) print ( "The original list 2 : " + str (test_list2)) # modifying tuple elements res = [] for i in range ( 0 , len (test_list1)): x = (test_list1[i][ 0 ],test_list2[i]) res.append(x) # print result print ( "The modified resultant list of tuple : " + str (res)) |
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
# Python3 code to demonstrate # modifying tuple elements # using zip() + list comprehension # initializing lists test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] test_list2 = [ 4 , 5 , 6 ] # printing original lists print ("The original list 1 : " + str (test_list1)) print ("The original list 2 : " + str (test_list2)) # using zip() + list comprehension # modifying tuple elements res = [(i[ 0 ], j) for i, j in zip (test_list1, test_list2)] # print result print ("The modified resultant list of tuple : " + str (res)) |
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
# Python3 code to demonstrate # modifying tuple elements # using zip() + map() + operator.itemgetter() import operator # initializing lists test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] test_list2 = [ 4 , 5 , 6 ] # printing original lists print ("The original list 1 : " + str (test_list1)) print ("The original list 2 : " + str (test_list2)) # using zip() + map() + operator.itemgetter() # modifying tuple elements temp = map (operator.itemgetter( 0 ), test_list1) res = list ( zip (temp, test_list2)) # print result print ("The modified resultant list of tuple : " + str (res)) |
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 # Initializing lists test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] test_list2 = [ 4 , 5 , 6 ] # Printing original lists print ( "The original list 1:" , test_list1) print ( "The original list 2:" , test_list2) # Modifying tuple elements using itertools.zip_longest() res = [(tup1[ 0 ], tup2) for tup1, tup2 in itertools.zip_longest(test_list1, test_list2)] # Print result print ( "The modified resultant list of tuple:" , res) #This code is contributed by Edula Vinay Kumar Reddy |
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): # Create an empty list to store the modified tuples. result = [] # Loop through each tuple in the first list. for i, (x, y) in enumerate (list1): # Get the i-th value from the second list, and use it to modify the y value in the current tuple. result.append((x, list2[i])) # Return the list of modified tuples. return result list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] list2 = [ 4 , 5 , 6 ] result = modify_tuples(list1, list2) print (result) |
[('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
# initializing lists test_list1 = [( 'Geeks' , 1 ), ( 'for' , 2 ), ( 'Geeks' , 3 )] test_list2 = [ 4 , 5 , 6 ] # printing original lists print ( "The original list 1 : " + str (test_list1)) print ( "The original list 2 : " + str (test_list2)) # using map() and lambda function to modify tuple elements res = list ( map ( lambda x, y: (x[ 0 ], y), test_list1, test_list2)) # print result print ( "The modified resultant list of tuple : " + str (res)) #This code is contributed by Jyothi pinjala |
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.