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 K can be multiplied to Nth element of tuple in list.
Method #1 : Using loop Using loops this task can be performed. In this, we just iterate the list to change the Nth element by predefined value K in code.
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
res = []
for i in range ( 0 , len (test_list)):
res.append((test_list[i][ 0 ], test_list[i][N] * K, test_list[i][ 2 ]))
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
Output :
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. loop performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2: Using list comprehension
This method is having the same approach as the above method, just reduces lines of code using list comprehension functionality to make code compact by size.
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
res = [(a, b * K, c) for a, b, c in test_list]
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
Output :
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]
The time complexity of this code is O(n), where n is the length of the input list test_list, as the code iterates through the list once to create the new list res.
The space complexity is also O(n), as a new list res is created that has the same length as the input list test_list.
Method #3: Using map()
This method uses the built-in map() function to apply a lambda function to each tuple in the list, which multiplies the Nth element by the constant value K.
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
res = list ( map ( lambda x: (x[ 0 ], x[N] * K, x[ 2 ]), test_list))
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
Output
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list slicing and unpacking.
Approach:
- Initialize a new empty list to store the modified tuples.
- Loop through each tuple in the original list.
- Slice the tuple to extract its first, second, and third elements.
- Multiply the second element with K and replace it in the sliced tuple.
- Pack the modified tuple back and append it to the new list.
- Return the new list.
Below is the implementation of the above approach:
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
res = []
for tup in test_list:
first, second, third = tup[:N], tup[N] * K, tup[N + 1 :]
modified_tup = ( * first, second, * third)
res.append(modified_tup)
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
Output
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]
The time complexity of this approach is O(n), where n is the number of tuples in the input list.
The auxiliary space complexity is also O(n), since we’re creating a new list to store the modified tuples.
Method 5: Using the numpy library.
Here are the steps to implement this method:
- Import the numpy library.
- Convert the list of tuples test_list to a numpy array.
- Multiply the Nth column of the array by K using the numpy.multiply() function.
- Replace the Nth column of the array with the modified column.
- Convert the numpy array back to a list of tuples using the tolist() method.
Python3
import numpy as np
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
arr = np.array(test_list)
arr[:,N] = np.multiply(arr[:,N], K)
res = arr.tolist()
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
Output:
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [[4, 15, 6], [7, 12, 2], [9, 30, 11]]
Time complexity: O(NM), where N is the number of tuples and M is the number of elements in each tuple. This is because we need to iterate over all the elements in each tuple and multiply the Nth element by K.
Auxiliary space: O(NM), where N is the number of tuples and M is the number of elements in each tuple. This is because we are creating a numpy array with the same shape as the original list of tuples.
Method 6: Using pandas library
- Import the pandas library.
- Convert the original list to a pandas DataFrame using the pd.DataFrame() function.
- Multiply the Nth column by K using DataFrame multiplication.
- Convert the result back to a list of tuples using the DataFrame.values.tolist() function.
Python3
import pandas as pd
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 1
K = 3
df = pd.DataFrame(test_list)
df[N] = df[N] * K
res = df.values.tolist()
print ( "The tuple after multiplying K to Nth element : " + str (res))
|
OUTPUT:
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [[4, 15, 6], [7, 12, 2], [9, 30, 11]]
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), where n is the number of elements in the list (due to the conversion to a DataFrame and back).
Share your thoughts in the comments
Please Login to comment...