Python | Summation of tuples in list
Last Updated :
02 May, 2023
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))
OutputThe 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))
OutputThe 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
OutputThe 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 :
- Initialize a list of tuples test_list with three tuples containing two, three, and two elements, respectively.
- Print the original list of tuples.
- 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.
- Use sum() function again to add up all the sums of elements from each tuple, resulting in the overall
- summation of elements in all tuples.
- 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))
OutputThe 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
- 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.
- Call the reduce() function with the lambda function sum_lambda, the list of tuples test_list, and an initial value of 0.
- 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.
- 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))
OutputThe 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
- Initiate a nested for loop ,the outer for loop is to traverse the list and inner for loop is to traverse each tuple
- With in inner for loop we will be adding each tuple element to sum variable(res)
- 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))
OutputThe 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)
Similar Reads
Python | Grouped summation of tuple list
Many times, we are given a list of tuples and we need to group its keys and perform certain operations while grouping. The most common operation is addition. Let's discuss certain ways in which this task can be performed. Apart from addition, other operations can also be performed by doing small cha
10 min read
Python | Summation of two list of tuples
Sometimes, while working with Python records, we can have a problem in which we need to perform cross-summation of list of tuples. This kind of application is popular in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip
6 min read
Python | Column summation of tuples
Sometimes, we encounter a problem where we deal with a complex type of matrix column summation in which we are given a tuple and we need to perform the summation of its like elements. This has a good application in Machine Learning domain. Let's discuss certain ways in which this can be done. Method
7 min read
Sort Tuple of Lists in Python
The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
3 min read
Python | Summation of Kth Column of Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of its Kth index. This problem has applications in the web development domain while working with data information. Let's discuss certain ways in which this task ca
7 min read
Python | Summation of dictionary list values
Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
6 min read
Python - Index Value Summation List
To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai
4 min read
Python | List of tuples to String
Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed. Method #1: Using str() + str
8 min read
Python | Mutual tuple subtraction in list
Sometimes, while working with data, we can have a problem in which we need to perform tuple subtraction among all the tuples in list. This can have application in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension This
3 min read
Python - Absolute Tuple Summation
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the summation of absolute values of intermediate tuple elements. This kind of problem can have application in many domains such as web development. Let's discuss certain ways in which this task can be perf
6 min read