Python | Nth tuple index Subtraction by K
Last Updated :
02 Jun, 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 K can be subtracted 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 the predefined value K in code.
Python3
# Python3 code to demonstrate working of
# Nth tuple element Subtraction by K
# Using loop
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 1
# Initializing K
K = 3
# Nth tuple element Subtraction by K
# Using loop
res = []
for i in range(0, len(test_list)):
res.append((test_list[i][0], test_list[i][N] - K, test_list[i][2]))
# printing result
print("The tuple after removing K from Nth element : " + str(res))
Output : The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after removing K from Nth element : [(4, 2, 6), (7, 1, 2), (9, 7, 11)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input 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
# Python3 code to demonstrate working of
# Nth tuple element Subtraction by K
# Using list comprehension
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 1
# Initializing K
K = 3
# Nth tuple element Subtraction by K
# Using list comprehension
res = [(a, b - K, c) for a, b, c in test_list]
# printing result
print("The tuple after removing K from Nth element : " + str(res))
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after removing K from Nth element : [(4, 2, 6), (7, 1, 2), (9, 7, 11)]
Time Complexity: O(n) where n is the number of elements in the string list. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res test_list.
Method #3 : Using map()
This method is also having the same approach as the above two methods but uses the map() function to make the code concise.
Python3
# Python3 code to demonstrate working of
# Nth tuple element Subtraction by K
# Using map()
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 1
# Initializing K
K = 3
# Nth tuple element Subtraction by K
# Using map()
res = list(map(lambda x: (x[0], x[N] - K, x[2]), test_list))
# printing result
print("The tuple after removing K from Nth element : " + str(res))
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after removing K from Nth element : [(4, 2, 6), (7, 1, 2), (9, 7, 11)]
Time complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using list slicing and unpacking
Use simple list slicing and unpacking techniques to achieve the same result.
Python3
# Python3 code to demonstrate working of
# Nth tuple element Subtraction by K
# Using list slicing and unpacking
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 1
# Initializing K
K = 3
# Nth tuple element Subtraction by K
# using list slicing and unpacking
res = [(t[0], t[N]-K, t[2]) for t in test_list]
# Printing result
print("The tuple after removing K from Nth element : " + str(res))
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after removing K from Nth element : [(4, 2, 6), (7, 1, 2), (9, 7, 11)]
Time Complexity: O(n), where n is the length of the input list. This is because we are iterating through the input list once.
Auxiliary Space: O(n), where n is the length of the input list. This is because we are creating a new list to store the modified tuples.
Method #5: Using NumPy library
Algorithm:
Convert the input list of tuples to a NumPy array. Subtract the value of K from the Nth element of each tuple in the array. Convert the resulting array back to a list of tuples. Return the list of tuples.
Python3
import numpy as np
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
N = 1
K = 3
# Convert list of tuples to NumPy array
arr = np.array(test_list)
# Subtract K from Nth element of each tuple
arr[:, N] -= K
# Convert NumPy array back to list of tuples
res = arr.tolist()
print("The tuple after subtracting K from Nth element : ", res)
Output:
The tuple after subtracting K from Nth element : [[4, 2, 6], [7, 1, 2], [9, 7, 11]]
Time Complexity: O(n), where n is the number of tuples in the input list. Converting the list to a NumPy array takes O(n) time, and subtracting K from the Nth element of each tuple also takes O(n) time. Converting the resulting array back to a list takes O(n) time.
Auxiliary Space Complexity: O(n), where n is the number of tuples in the input list. Converting the list to a NumPy array requires creating a new array, which takes O(n) space. Converting the resulting array back to a list also requires creating a new list, which takes O(n) space.
Similar Reads
Python | Nested Tuples Subtraction Sometimes, while working with records, we can have a problem in which we require to perform index-wise subtraction of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuple. Letâs discuss certain ways in which this problem can be solved. Metho
7 min read
Python | Mutual tuple subtraction in list Sometimes, while working with data, we can have a problem in which we need to perform tuple subtraction among all the tuples in list. This can have application in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension This
3 min read
Python | How to get Subtraction of tuples Sometimes, while working with records, we might have a common problem of subtracting the contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Letâs discuss certain ways in which this task can be perform
5 min read
Python - Subtract K from tuples list Sometimes, while working with data, we can have a problem in which we need to perform update operation on tuples. This can have application across many domains such as web development. Letâs discuss certain ways in which subtraction of K can be performed. Method #1 : Using list comprehension This is
4 min read
Python - Trim tuples by K Given the Tuple list, trim each tuple by K. Examples: Input : test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)], K = 2 Output : [(2,), (9,), (2,), (2,)] Explanation : 2 elements each from front and rear are removed. Input : test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1)
3 min read