Python – Absolute Tuple Summation

Sometimes, while working with Python tuples, we can have a problem in which we need to perform the summation of absolute values of intermediate tuple elements. This kind of problem can have application in many domains such as web development. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(-7, -8), (-5, -6)]
Output : [15, 11]

Input : test_list = [(-1, -2, -4)]
Output : [7]

Method #1 : Using sum() + list comprehension + abs()
The combination of above functions can be used to solve this problem. In this, we perform the task of computing sum using sum(), abs() for absolute values, and list comprehension to iterate through the list and inside each tuple, generator expression is used to iterate.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Absolute Tuple Summation
# Using sum() + list comprehension + abs()
  
# initializing list
test_list = [(7, -8), (-5, -6), (-7, 2), (6, 8)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Absolute Tuple Summation
# Using sum() + list comprehension + abs()
res = [sum([abs(ele) for ele in sub]) for sub in test_list]
  
# printing result 
print("The absolute sum list: " + str(res)) 

chevron_right


Output :



The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]

 

Method #2 : Using list comprehension + sum() + map()
The combination of above functions offer an alternative to solve this problem. In this, we perform the task of computing sum of entire tuple elements using map() rather than generator expression.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Absolute Tuple Summation
# Using list comprehension + sum() + map()
  
# initializing list
test_list = [(7, -8), (-5, -6), (-7, 2), (6, 8)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Absolute Tuple Summation
# Using list comprehension + sum() + map()
res = [sum(map(abs, ele)) for ele in test_list]
  
# printing result 
print("The absolute sum list: " + str(res)) 

chevron_right


Output :

The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.