How to sum the products of a tuple in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while programming, we have a problem in which we might need to perform summation among tuple elements. This is an essential utility as we come across summation operations many times and tuples are immutable and hence required to be dealt with. Let’s discuss certain ways in which this task can be performed. 

    Method #1 : Using list() + sum() The above functions can be combined to perform this task. We can employ sum() to accumulate the result of summation logic. The list() function is used to perform interconversions. 

    Python3

    test_tup = (7, 8, 9, 1, 10, 7)

    print("The original tuple is : " + str(test_tup))

    res = sum(list(test_tup))

    print("The summation of tuple elements are : " + str(res))

    Output : 

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

    Method #2 : Using map() + sum() + list() The combination of above functions can be used to perform this task. In this, we first convert the tuple to list, flatten it’s each list element using map(), perform summation of each using sum() and again employ sum() for overall summation of the resultant list. 

    Python3

    test_tup = ([7, 8], [9, 1], [10, 7])

    print("The original tuple is : " + str(test_tup))

    res = sum(list(map(sum, list(test_tup))))

    print("The summation of tuple elements are : " + str(res))

    Output : 

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

    Method #3: Using for loop

    Python3

    test_tup = (7, 8, 9, 1, 10, 7)

    print("The original tuple is : " + str(test_tup))

    res=0

    for i in test_tup:

        res+=i

    print("The summation of tuple elements are : " + str(res))

    Output

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


    Sum the elements of a tuple in Python #

    Use the sum() function to sum the elements of a tuple, e.g. result = sum(my_tuple). The sum function takes an iterable (such as a tuple) as an argument, sums its items from left to right and returns the total.

    Copied!

    my_tuple = (10, 20, 30) # ✅ sum the elements of a tuple result = sum(my_tuple) print(result) # 👉️ 60 # -------------------------------- # ✅ sum the elements of each tuple in a list of tuples list_of_tuples = [(10, 20), (30, 40), (50, 60)] result_2 = [sum(t) for t in list_of_tuples] print(result_2) # 👉️ [30, 70, 110] # ---------------------------------- # ✅ sum the elements at index `0` of each tuple in a list result_3 = sum(tup[0] for tup in list_of_tuples) print(result_3) # 👉️ 90

    We used the sum() function to sum the elements of a tuple.

    Copied!

    my_tuple = (10, 20, 30) result = sum(my_tuple) print(result) # 👉️ 60

    The sum function takes an iterable, sums its items from left to right and returns the total.

    The function takes the following 2 arguments:

    NameDescription
    iterable the iterable whose items to sum
    start sums the start value and the items of the iterable. sum defaults to 0 (optional)

    If you need to sum the elements of each tuple in a list, use a list comprehension.

    Copied!

    # ✅ sum the elements of each tuple in a list of tuples list_of_tuples = [(10, 20), (30, 40), (50, 60)] result_2 = [sum(t) for t in list_of_tuples] print(result_2) # 👉️ [30, 70, 110]

    List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

    In the example, we iterate over the list and pass each tuple to the sum() function to sum its elements.

    The first tuple has a sum of 30, the second a sum of 70, and the third a sum of 110.

    If you need to sum the elements at index N of each tuple in a list, use a generator expression.

    Copied!

    # ✅ sum the elements at index `0` of each tuple in a list list_of_tuples = [(10, 20), (30, 40), (50, 60)] result_3 = sum(tup[0] for tup in list_of_tuples) print(result_3) # 👉️ 90

    Generator expressions are also used to perform some operation for every element or select a subset of elements that meet a condition.

    On each iteration, we access the tuple element at index 0 (the first tuple item) and return the result.

    The example passes a generator object that contains the numbers 10, 30, 50 to the sum() function which then returns 90.

    How do you sum items in a list in Python?

    Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

    How do you sum two tuples?

    To add two tuples element-wise: Use the zip function to get an iterable of tuples with the corresponding items. Use a list comprehension to iterate over the iterable. On each iteration, pass the tuple to the sum() function.

    What is sum () sum () in Python?

    The Python sum() function calculates the total of all numerical values in an iterable. sum() works with both integers and floating-point numbers. The sum() function has an optional parameter to add a number to the total.

    How do you print a sum in Python?

    How to Add Two Numbers in Python.
    ❮ Previous Next ❯.
    Example. x = 5. y = 10. print(x + y) Try it Yourself ».
    Example. x = input("Type a number: ") y = input("Type another number: ") sum = int(x) + int(y) print("The sum is: ", sum) Try it Yourself ».
    ❮ Previous Next ❯.