Open In App

Python | Summation of tuples in list

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using sum() + map() 

A combination of the above functions can be used to solve this particular problem. In this the task of summation is performed by sum(), and applying the summation functionality to each element in tuple list is performed by map() method. 

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
# using sum() + map()
 
# initialize list of tuple
test_list = [(1, 3), (5, 6, 7), (2, 6)]
 
# printing original tuples list
print("The original list : " + str(test_list))
 
# Summation of tuples in list
# using sum() + map()
res = sum(map(sum, test_list))
 
# printing result
print("The summation of all tuple elements are : " + str(res))


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements are : 30

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using sum() + izip() 

The combination of above functions can be used to perform this particular task. In this, we perform the task of map() using izip(). It helps to club all the elements for summation by sum(). Works only for single element tuple and only with Python2. 

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
# using sum() + izip()
 
from itertools import izip
 
# initialize list of tuple
test_list = [(1, ), (5, ), (2, )]
 
# printing original tuples list
print("The original list : " + str(test_list))
 
# Summation of tuples in list
# using sum() + map()
res = sum(*izip(*test_list))
 
# printing result
print("The summation of all tuple elements are : " + str(res))


Output:

The original list : [(1, ), (5, ), (2, )]
The summation of all tuple elements are : 8

Method #3 : Using sum(),list() methods and for loop

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
 
# initialize list of tuple
test_list = [(1, 3), (5, 6, 7), (2, 6)]
 
# printing original tuples list
print("The original list : " + str(test_list))
 
# Summation of tuples in list
res = 0
for i in test_list:
    res += sum(list(i))
 
# printing result
print("The summation of all tuple elements are : " + str(res))


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements are : 30

Time complexity: O(n*m), where n is the number of tuples in the list and m is the maximum length of any tuple in the list. 

Auxiliary space complexity: O(1), since the code only uses a single variable res to store the sum of all tuple elements, and does not create any additional data structures.

Method #4 : Using reduce() + add()

The combination of above functions can be used to perform this particular task. In this, we use reduce() function to iterate through the tuples in the list and add() function from operator module to perform the summation.

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
# using reduce() + add()
  
from operator import add
from functools import reduce
 
# initialize list of tuple
test_list = [(1, 3), (5, 6, 7), (2, 6)]
  
# printing original tuples list
print("The original list : " + str(test_list))
  
# Summation of tuples in list
# using reduce() + add()
res = reduce(add, [sum(tup) for tup in test_list])
  
# printing result
print("The summation of all tuple elements are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements are : 30

Time complexity: O(n) where n is the number of tuples in the list
Auxiliary Space: O(1) as we are using variables res and add function.

Method 5: Using a list comprehension

Step by step approach :

  1. Initialize a list of tuples test_list with three tuples containing two, three, and two elements, respectively.
  2. Print the original list of tuples.
  3. Use list comprehension to iterate over the tuples in test_list. For each tuple i, use the sum() function to add up all the elements in the tuple.
  4. Use sum() function again to add up all the sums of elements from each tuple, resulting in the overall
  5. summation of elements in all tuples.
  6. Print the resulting sum of elements.

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
 
# initialize list of tuple
test_list = [(1, 3), (5, 6, 7), (2, 6)]
 
# printing original tuples list
print("The original list : " + str(test_list))
 
# Summation of tuples in list using list comprehension
res = sum([sum(i) for i in test_list])
 
# printing result
print("The summation of all tuple elements are : " + str(res))


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements are : 30

Time Complexity: O(n), where n is the total number of elements in the list of tuples.
Auxiliary Space: O(1),

Method 6: Using the ‘reduce()’ function from the ‘functools’ module

Step-by-step algorithm for implementing the approach

  1. Define a lambda function sum_lambda that takes in two arguments, an accumulator x and a tuple y. The lambda function computes the sum of the elements in the tuple y and adds it to the accumulator x.
  2. Call the reduce() function with the lambda function sum_lambda, the list of tuples test_list, and an initial value of 0.
  3. The reduce() function will apply the lambda function to the first two elements of the list, then to the result and the next element, and so on, until it reduces the entire list to a single value.
  4. Return the final result.

Python3




from functools import reduce
 
# initialize list of tuples
test_list = [(1, 3), (5, 6, 7), (2, 6)]
print("The original list : " + str(test_list))
# define the lambda function to add the sum of each tuple to a running total
sum_lambda = lambda x, y: x + sum(y)
 
# apply the reduce() function with the lambda function and initial value of 0
res = reduce(sum_lambda, test_list, 0)
 
# print the result
print("The summation of all tuple elements is: " + str(res))


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements is: 30

Time complexity:

The lambda function sum_lambda takes O(k) time to compute the sum of k integers in a tuple.
The reduce() function applies the lambda function to each element in the list of tuples, so the total time complexity of reduce() is O(nk), where n is the number of tuples and k is the average length of each tuple.
Therefore, the overall time complexity of the algorithm is O(nk).
Auxiliary space:

The reduce() function uses O(1) auxiliary space, as it only needs to keep track of the accumulator and the current element being processed.
Therefore, the overall auxiliary space complexity of the algorithm is O(1).
The time and auxiliary space complexity of this algorithm is already quite efficient at O(nk) and O(1) respectively, which means that it can handle large inputs efficiently. Therefore, there is no need for improvement request at this time.

Method #7 : Using nested for loops

Approach

  1. Initiate a nested for loop ,the outer for loop is to traverse the list and inner for loop is to traverse each tuple
  2. With in inner for loop we will be adding each tuple element to sum variable(res)
  3. Display res after exiting from the loops

Python3




# Python3 code to demonstrate working of
# Summation of tuples in list
 
 
# initialize list of tuple
test_list = [(1, 3), (5, 6, 7), (2, 6)]
 
# printing original tuples list
print("The original list : " + str(test_list))
 
# Summation of tuples in list
res=0
for i in test_list:
    for j in i:
        res+=j
 
# printing result
print("The summation of all tuple elements are : " + str(res))


Output

The original list : [(1, 3), (5, 6, 7), (2, 6)]
The summation of all tuple elements are : 30

Time Complexity : O(N*M) N -length of list M -length of each tuple

Auxiliary Space : O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads