Python - Summation in Dual element Records List
Last Updated :
05 Apr, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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 :
- Import the NumPy library using the statement import numpy as np.
- Initialize a list of tuples, test_list, containing the dual element records whose summation is to be computed.
- Print the original list using the statement print("The original list is : " + str(test_list)).
- 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.
- 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:
- Initialize the list test_list with tuples of length 2.
- Initialize an empty list res to store the summation of pairs in test_list.
- Iterate over all possible combinations of length 1 in test_list using itertools.combinations() function.
- For each combination, calculate the sum of the elements in the tuple using the sum() function and append it to res.
- 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.
OutputThe 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).
Similar Reads
Python | Alternate element summation in list
The problem of getting summation of a list is quite generic and we might some day face the issue of getting the summation of alternate elements and get the list of 2 elements containing summation of alternate elements. Let's discuss certain ways in which this can be performed. Method #1 : Using list
5 min read
Element indices Summation - Python
Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40
3 min read
Python | Record elements Average in List
Given a list of tuples, write a program to find average of similar tuples in list. Examples: Input: [('Geeks', 10), ('For', 10), ('Geeks', 2), ('For', 9), ('Geeks', 10)] Output: Resultant list of tuples: [('For', 9.5), ('Geeks', 7.333333333333333)] Input: [('Akshat', 10), ('Garg', 10), ('Akshat', 2)
3 min read
Python - Summation after elements removal
Sometimes we need to perform the operation of removing all the items from the lists that are present in other list, i.e we are given some of the invalid numbers in one list which needs to be get ridden from the original list and perform its summation. Lets discuss various ways in which this can be p
5 min read
Python | Common Row elements Summation
The problem of finding the common elements in the list of 2 lists is quite a common problem and can be dealt with with ease and also has been discussed many times. But sometimes, we require to find the elements that are in common with N lists and return their sum. Let us discuss certain ways in whic
6 min read
Update Each Element in Tuple List - Python
The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read
Python | Pair summation of list elements
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve
4 min read
Python | Three element sum in list
The problem of getting the number of pairs that lead to a particular solution has been dealt with many times, this article aims at extending that to 3 numbers and discussing several ways in which this particular problem can be solved. Let's discuss certain ways in which this can be performed.Method
10 min read
Python | Equal Keys List Summation
Sometimes, while working with dictionaries, we can have a problem in which we have many dictionaries and we are required to sum like keys. This problem seems common, but complex is if the values of keys are list and we need to add elements to list of like keys. Letâs discuss way in which this proble
4 min read
Python | Column summation in uneven sized lists
The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the summation of its columns, the uneven length of rows may lead to some elements in that element to be absent and if not handled correctly, may throw exception. L
7 min read