Sometimes, while working with data, we can have a problem in which we need to perform update operations on tuples. This can have applications across many domains such as web development. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This is shorthand to brute function that can be applied to perform this task. In this, we iterate for each element of each tuple to perform bulk update of data.
Python3
# Python3 code to demonstrate working of # Update each element in tuple list # Using list comprehension # initialize list test_list = [( 1 , 3 , 4 ), ( 2 , 4 , 6 ), ( 3 , 8 , 1 )] # printing original list print ("The original list : " + str (test_list)) # initialize add element add_ele = 4 # Update each element in tuple list # Using list comprehension res = [ tuple (j + add_ele for j in sub ) for sub in test_list] # printing result print (" List after bulk update : " + str (res)) |
The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)] List after bulk update : [(5, 7, 8), (6, 8, 10), (7, 12, 5)]
Method #2 : Using map() + lambda + list comprehension The combination of above functions can be used to perform this task. In this, we just iterate for all elements using map() and extend logic of update using lambda function.
Python3
# Python3 code to demonstrate working of # Update each element in tuple list # Using list comprehension + map() + lambda # initialize list test_list = [( 1 , 3 , 4 ), ( 2 , 4 , 6 ), ( 3 , 8 , 1 )] # printing original list print ("The original list : " + str (test_list)) # initialize add element add_ele = 4 # Update each element in tuple list # Using list comprehension + map() + lambda res = [ tuple ( map ( lambda ele : ele + add_ele, sub)) for sub in test_list] # printing result print (" List after bulk update : " + str (res)) |
The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)] List after bulk update : [(5, 7, 8), (6, 8, 10), (7, 12, 5)]
Using a loop and tuple unpacking:
Approach:
Define the function update_tuples that takes in two arguments – the list of tuples and the new value.
Loop through each tuple in the list using the range of its length.
Unpack each tuple into three variables x, y, z.
Create a new tuple with the first element as the new value and the remaining elements as y and z.
Replace the original tuple with the new tuple in the same index.
Return the updated list of tuples.
Define the list of tuples tuples and the new value new_val.
Call the function update_tuples with the arguments tuples and new_val.
Print the updated list of tuples.
Python3
def update_tuples(tuples, new_val): for i in range ( len (tuples)): x, y, z = tuples[i] tuples[i] = (new_val, y, z) return tuples tuples = [( 1 , 56 , 'M' ), ( 1 , 14 , 'F' ), ( 2 , 43 , 'F' ), ( 2 , 10 , 'M' )] new_val = 5 updated_tuples = update_tuples(tuples, new_val) print (updated_tuples) |
[(5, 56, 'M'), (5, 14, 'F'), (5, 43, 'F'), (5, 10, 'M')]
Time complexity: O(n), where n is the length of the list of tuples.
Auxiliary Space: O(1).
METHOD 4:USING FOR.
APPROACH:
The approach used in this code is to iterate over each tuple in the list, convert it to a mutable list using the list() function, update the value at the specified index, convert the list back to a tuple using the tuple() function, and append the updated tuple to a new list. The new list contains all the updated tuples.
ALGORITHM:
1.Create a new list with updated tuples.
2.Iterate over each tuple in the original list and create a new tuple with updated element(s).
3.Append the new tuple to the new list.
Python3
test_list = [( 1 , 3 , 4 ), ( 2 , 4 , 6 ), ( 3 , 8 , 1 )] index = 1 value = 5 new_list = [] for tup in test_list: temp_list = list (tup) temp_list[index] = value new_tup = tuple (temp_list) new_list.append(new_tup) print ( "Updated list:" , new_list) |
Updated list: [(1, 5, 4), (2, 5, 6), (3, 5, 1)]
Time Complexity: O(n)
Auxiliary Space: O(n)