Open In App

Python – Summation in Dual element Records List

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Records list, we can have problem in which we perform the summation of dual tuple records and store it in list. This kind of application can occur over various domains. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension This is one of the way to solve this problem. In this we perform summation of dual tuples in a list and iteration is performed inside comprehended list. 

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
# using list comprehension
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List
# using list comprehension
res = [ele[0] + ele[1] for ele in test_list]
 
# printing result
print ("Summation pairs in tuple list : " + str(res))


Output : 

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time complexity: O(n), where n is the number of elements in the input list “test_list”.
Auxiliary space: O(n), where n is the number of elements in the input list “test_list”. This is because a new list of size n is created to store the result of the summation.

Method #2 : Using reduce() + add() This is yet another way to perform this task. In this, we iterate through the list and perform summation using reduce() and add() respectively. 

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
# using reduce() + add()
from operator import add
from functools import reduce
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List
# using reduce() + add()
res = [reduce(add, sub, 0) for sub in test_list]
 
# printing result
print ("Summation pairs in tuple list : " + str(res))


Output : 

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time complexity: O(n), where n is the number of elements in the input list test_list. This is because the reduce() function takes O(n) time to process the elements in each tuple, and the for loop takes O(n) time to process all n tuples in test_list.
Auxiliary space: O(n), where n is the number of elements in the input list test_list. This is because the res list, which contains the sums of each tuple in test_list, takes up O(n) space.

Method #3 : Using sum(),list() methods

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List
res=[]
for i in test_list:
    res.append(sum(list(i)))
     
# printing result
print ("Summation pairs in tuple list : " + str(res))


Output

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time complexity: O(n), where n is the number of elements in the input list. 
Auxiliary space: O(n), where n is the number of elements in the input list.

Method #4: Using map() function 

The map() function applies the lambda function to each element of the test_list. In this case, the lambda function takes a tuple and returns the sum of the elements in the tuple.

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List using map() function
res = list(map(lambda x: sum(x), test_list))
 
# printing result
print("Summation pairs in tuple list : " + str(res))


Output

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time complexity: O(n), where n is the number of elements in the test_list. 
Auxiliary space: O(n). This is because we are creating a new list res to store the sums of the tuples, which has the same length as the test_list.

Method #5: Using a for loop

This program calculates the sum of two elements in each tuple of a given list using a for loop, and stores the result in a new list. Finally, it prints the list of sums.

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
# using a for loop
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List
# using a for loop
res = []
for ele in test_list:
    res.append(ele[0] + ele[1])
 
# printing result
print ("Summation pairs in tuple list : " + str(res))


Output

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as the method creates a new list res to store the results. 

Method 6: Using Numpy

In this approach, we use the numpy.sum() function to sum up the elements in each tuple of the given list along the axis 1. The result is a NumPy array containing the sums of the tuples. We can then convert this array to a list if needed.

step-by-step approach :

  1. Import the NumPy library using the statement import numpy as np.
  2. Initialize a list of tuples, test_list, containing the dual element records whose summation is to be computed.
  3. Print the original list using the statement print(“The original list is : ” + str(test_list)).
  4. Compute the summation of each tuple in the list using the NumPy sum() function with the axis parameter set to 1, which indicates that we want to sum along the second axis (columns). Store the result in a variable named res.
  5. Print the result using the statement print(“Summation pairs in tuple list : ” + str(res)).

Python3




# Python3 code to demonstrate
# Summation in Dual element Records List
# using numpy.sum()
 
import numpy as np
 
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Summation in Dual element Records List
# using numpy.sum()
res = np.sum(test_list, axis=1)
 
# printing result
print ("Summation pairs in tuple list : " + str(res))


OUTPUT:

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13  6 17  8]

The time and space complexity of this approach is O(n), where n is the length of the given list. 

The space complexity is O(n) as well, as we need to store the entire list and the result array in memory. 

Method 7: Using itertools:

Algorithm:

  1. Initialize the list test_list with tuples of length 2.
  2. Initialize an empty list res to store the summation of pairs in test_list.
  3. Iterate over all possible combinations of length 1 in test_list using itertools.combinations() function.
  4. For each combination, calculate the sum of the elements in the tuple using the sum() function and append it to res.
  5. Print the original list and the resultant list.

Python3




# Python program for the above approach
import itertools
 
# Driver Code
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
 
res = []
for pair in itertools.combinations(test_list, 1):
    res.append(sum(pair[0]))
 
print("The original list is : " + str(test_list))
print("Summation pairs in tuple list : " + str(res))
 
# This code is contributed by Jyothi pinjala.


Output

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

Time Complexity: The time complexity of this code is O(n^2), where n is the length of test_list. This is because itertools.combinations() function takes O(n^2) time to generate all possible combinations of length 1 and the sum() function takes O(1) time to calculate the sum of the elements in each tuple. Hence, the overall time complexity is O(n^2).

Space Complexity: The space complexity of this code is O(n), where n is the length of test_list. This is because we are creating a new list res of size n to store the summation of pairs in test_list. The space complexity of the for loop is constant. Hence, the overall space complexity is O(n).



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads