ADVERTISEMENT
ADVERTISEMENT

Python | Sum of tuple elements

In this program, we have a tuple consisting of integer elements. Our task is to create a Python program to find the sum of tuples elements.
Submitted by Shivang Yadav, on November 06, 2021

Utility functions like finding the sum, average, and performing other operations on all the elements of a collection are pretty useful while working with data structures in python. Summation on tuples is an operation that is performed quite often while working with integer tuples and has found applications in many areas. In this article, we will see a Python program to find the sum of all elements of tuples.

Input:
myTuple = (3, 1, 7, 8)

Output:
19

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)

Finding the sum of tuple elements

To solve the problem, we will be traversing the tuples and then adding all them and returning the sum.

As python provides more than one way to perform the given task to the programmer in order to provide them with options to choose from based on the requirement of the program. Here, also we have ways to find the sum of all elements of tuples.

Method 1:

One method to solve the problem is by using the sum of all elements of the tuple using the sum() method. As the sum() method is applied only on lists, we need to convert the tuple to a list which is done using the list() method.

# Python program to find the 
# sum of tuple elements 
  
# Creating and printing the 
# tuples of integer values 
myTuple = (7, 8, 9, 1, 10, 7) 
  
# printing original tuple
print("The original tuple is : " + str(myTuple)) 
  
# finding sum of all tuple elements
tupSum = sum(list(myTuple))
  
# Printing the Tuple sum  
print("The summation of tuple elements are : " + str(tupSum))

Output:

The original tuple is : (7, 8, 9, 1, 10, 7)
The summation of tuple elements are : 42

Method 2:

One more method to solve the problem is by using a combination of the map(), sum(), and list() methods. This method can even find the summation of tuples that have nested collections inside them. The map method will be used to flatten the tuple and then sum to find the sum of the resulting flattened list.

# Python program to find the 
# sum of tuple elements 
  
# Creating and printing the tuples 
# of integer values 
myTuple = ([7, 8], [9, 1, 10, 7]) 
  
# printing original tuple
print("The original tuple is : " + str(myTuple)) 
  
# finding sum of all tuple elements
tupSum = sum(list(map(sum, list(myTuple))))
  
# Printing the Tuple sum  
print("The summation of tuple elements are : " + str(tupSum))

Output:

The original tuple is : ([7, 8], [9, 1, 10, 7])
The summation of tuple elements are : 42

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.