Python | Exponentiate Kth Record Index
Last Updated :
13 Apr, 2023
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 exponentiated to Kth 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 Kth element by predefined value N in code.
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 3
K = 1
res = []
for i in range ( 0 , len (test_list)):
res.append((test_list[i][ 0 ], test_list[i][K] * * N, test_list[i][ 2 ]))
print ( "The tuple after Exponentiating N to Kth element : " + str (res))
|
Output
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]
Time Complexity: O(n*n) where n is the number of elements in the string list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the string 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 = 3
K = 1
res = [(a, b * * N, c) for a, b, c in test_list]
print ( "The tuple after Exponentiating N to Kth element : " + str (res))
|
Output :
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using numpy
In this approach, we are using numpy library to perform the exponentiation on the Kth element of each tuple in the list. We first convert the original list of tuples into a numpy array, then we use the res[:,K] = res[:,K] ** N to perform the exponentiation on the Kth element of each tuple. The time complexity of this approach is O(n) and the space complexity is O(n).
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 = 3
K = 1
res = np.array(test_list)
res[:,K] = res[:,K] * * N
print ( "The tuple after Exponentiating N to Kth element : " + str (res))
|
Output:
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [[ 4 125 6]
[ 7 64 2]
[ 9 1000 11]]
Method #4 : Using math.pow() and tuple() methods
Approach
- Initiate a for loop to traverse list of tuples
- For each tuple change the middle element by raising it to the power of N
- Append the elements as it is to a list and convert it to a tuple
- Append tuple to output list
- Display output list
Python3
test_list = [( 4 , 5 , 6 ), ( 7 , 4 , 2 ), ( 9 , 10 , 11 )]
print ( "The original list is : " + str (test_list))
N = 3
K = 1
import math
res = []
for i in range ( 0 , len (test_list)):
x = []
a = math. pow (test_list[i][ 1 ],N)
x.append(test_list[i][ 0 ])
x.append( int (a))
x.append(test_list[i][ 2 ])
res.append( tuple (x))
print ( "The tuple after Exponentiating N to Kth element : " + str (res))
|
Output
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]
Time Complexity : O(M*N) M – length of tuples list N – length of each tuple
Auxiliary Space : O(M*N) M – length of tuples list N – length of each tuple
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...