Q:

Python program to perform summation of tuple in list

belongs to collection: Python Tuple Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to flatten tuple list to string... >>
<< Python program to concatenate tuples to nested tup...