Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get sum of nested tuples in list

I have the list [(270,), (270,)] and I want to add the values in the tuples together. How would I do this?

Note: The number of tuples will vary so I need a dynamic solution to add the values together

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you have a list of tuples (varying in size), you can use the sum function in Python directly:

Initialize a variable to store the total sum
total_sum = 0

# Initialize a variable to store the total sum
total_sum = 0

# Iterate through the list and add the values in each tuple to the total sum
for each_tup in tuple_list:
   total_sum += sum(each_tup)

print(total_sum)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading