Skip to content
Related Articles

Related Articles

Python – Absolute Tuple Summation

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Jul, 2020
View Discussion
Improve Article
Save Article

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.




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

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.




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

Output :

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!