Python - Subtract K from tuples list
Last Updated :
09 Apr, 2023
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 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 subtraction of K of data.
Python3
# Python3 code to demonstrate working of
# Subtract K from tuples 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
K = 4
# Subtract K from tuples list
# Using list comprehension
res = [tuple(j - K for j in sub ) for sub in test_list]
# printing result
print("List after subtraction of K : " + str(res))
OutputThe original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
List after subtraction of K : [(-3, -1, 0), (-2, 0, 2), (-1, 4, -3)]
Time Complexity: O(n*n), where n is the length of the tuple list. This is because we’re using the list comprehension 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 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
# Subtract K from tuples 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
K = 4
# Subtract K from tuples list
# Using list comprehension + map() + lambda
res = [tuple(map(lambda ele : ele - K, sub)) for sub in test_list]
# printing result
print("List after subtraction of K : " + str(res))
OutputThe original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
List after subtraction of K : [(-3, -1, 0), (-2, 0, 2), (-1, 4, -3)]
Time Complexity: O(n*n) where n is the number of elements in the string list. The map() + lambda + list comprehension is used to perform the task and it takes O(n*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 numpy
We can use numpy module to perform this task. In this, we perform subtraction of K using numpy.subtract() method.
Python3
# Python3 code to demonstrate working of
# Subtract K from tuples list
# Using numpy
# importing numpy
import numpy as np
# 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
K = 4
# Subtract K from tuples list
# Using numpy
res = [tuple(np.subtract(sub, K)) for sub in test_list]
# printing result
print("List after subtraction of K : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output :
The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
List after subtraction of K : [(-3, -1, 0), (-2, 0, 2), (-1, 4, -3)]
Time complexity: O(N)
Auxiliary Space: O(N)
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 | Nth tuple index Subtraction by K 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 t
6 min read
Python | Summation of Kth Column of Tuple List Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of its Kth index. This problem has applications in the web development domain while working with data information. Let's discuss certain ways in which this task ca
7 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