Python program to perform summation of tuple in list

In this problem, we are given a list of tuples consisting of integer values. Our task is to create a Python program to perform the summation of the elements of tuple in a list of tuples.
Submitted by Shivang Yadav, on January 10, 2022

Working with numbers and data in programming is complex. Sometimes it requires the extraction of data chunks after performing some operation. Summation, selective extraction, mean, etc are some common operations. And as a programmer, you should know how these operations are performed. Here is a program in python using which we can perform the summation of each element of the tuples present in the list of tuples in python.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Summation of tuple in list

We are given a list of tuples consisting of integer elements. We need to create a Python program to perform the summation of all elements of each tuple from the list of tuples.

Input:
[(2, 9) ,(5, 6), (1, 3, 4, 8)]

Output:
38

We will be creating a program that will flatter the tuple and create a string with the flattened elements. In Python, we have multiple methods and ways to perform this task.

Method 1:

One method to solve the problem is by first creating a map of sum of elements of each tuple in Python. Then sum up all elements of the map which will be our final result.

# Python program to perform summation of tuple 
# in list using sum and map

# Initializing and printing list of tuples
myList = [(2, 9) ,(5, 6), (1, 3, 4, 8) ]
print("The elements of the list are " + str(myList))

# Performing summation of elements of tuples in list
eleSum = sum(map(sum, myList))

print("The sum of all elements is " + str(eleSum))

Output:

The elements of the list are [(2, 9), (5, 6), (1, 3, 4, 8)]
The sum of all elements is 38

Method 2:

To perform this task, first convert the given tuple to a list, and then flatten list element using the map() function, then perform summation of each using the sum() function, and again employ sum() for overall summation of resultant list.

# Python program to perform summation 
# of tuple in list

# Initializing and printing list of tuples
myList = [(2, 9) ,(5, 6), (1, 3, 4, 8) ]
print("The elements of the list are " + str(myList))

# Performing summation of elements of tuples in list
eleSum = sum(list(map(sum, list(myList))))

print("The sum of all elements is " + str(eleSum))

Output:

The elements of the list are [(2, 9), (5, 6), (1, 3, 4, 8)]
The sum of all elements is 38

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.