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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.