Wednesday, November 20, 2024
Google search engine
HomeLanguagesPython | Nth tuple index Subtraction by K

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 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))


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) 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))


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)
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))


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), 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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments