Python | Summation of Kth Column of Tuple List
Last Updated :
06 Apr, 2023
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 can be performed.
Method #1: Using list comprehension + sum()
This task can be performed using the combination of above functionalities. In this, summation of index occurs using sum() and list comprehension drives the iteration and access of Nth index element of each tuple in list.
Python3
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
K = 2
res = sum ([sub[K] for sub in test_list])
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time Complexity: O(n), where n is the number of Tuples in given list.
Auxiliary Space: O(n)
Method #2: Using imap() + sum() + itemgetter()
The combination of above functions can also achieve this task. This approach is generator based and recommended in case we have a very large list. In this, sum() is used to perform summation, itemgetter to get Kth index and imap() performs the task of mapping elements to perform summation. Works only in Python2.
Python
from operator import itemgetter
from itertools import imap
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
K = 2
idx = itemgetter(K)
res = sum (imap(idx, test_list))
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1), because the amount of extra memory used by the algorithm is constant, regardless of the size of the input.
Method #3: Using numpy
Note: Install numpy module using command “pip install numpy”
Another approach to solve this problem is by using the numpy library. This can be done by converting the tuple list into a numpy array and then using numpy’s sum() function along with the index of the desired column.
Python3
import numpy as np
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
K = 2
res = np. sum (np.array(test_list)[:,K])
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output:
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time complexity: O(n) where n is the number of tuples in the list. It iterates through the list once to extract the values of the kth index.
Auxiliary space: O(n) as well, as it creates a new numpy array with n elements to store the kth index values.
Method #4: Using a for loop
- Initialize the list of tuples
- Print the original list
- Initialize the value of K
- Initialize the sum to zero
- Iterate over each tuple in the list
- Add the Kth element of the current tuple to the sum
- Print the result
Python3
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
K = 2
res = 0
for tup in test_list:
res + = tup[K]
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1).
Method #5: Using Recursive method.
Algorithm:
- Define a recursive function sum_kth_column_recursive that takes two arguments: tuples_list and k.
- If tuples_list is empty, return 0 (base case).
- Otherwise, add the Kth element of the first tuple in tuples_list to the sum, and call the function recursively with the rest of the list (recursive case).
- Return the final sum.
Python3
def sum_kth_column_recursive(tuples_list, k):
if not tuples_list:
return 0
return tuples_list[ 0 ][k] + sum_kth_column_recursive(tuples_list[ 1 :], k)
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
K = 2
res = sum_kth_column_recursive(test_list,K)
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time complexity: O(n), where n is the number of tuples in the list. In the worst case, we will need to traverse the entire list to compute the sum.
Auxiliary Space: O(n), where n is the number of tuples in the list. The space used by the recursive function call stack will be proportional to the number of tuples in the list.
Method 6: Using the built-in function reduce() from the functools module.
Step-by-step approach:
- The reduce() function from the functools module is used to apply a function to each tuple in the list, accumulating the sum of the k-th element so far.
- The function passed to reduce() takes two arguments: acc, which is the accumulated sum so far, and tpl, which is the current tuple being processed.
- The lambda function returns the sum of the accumulated sum so far (acc) and the k-th element of the current tuple (tpl[k]).
- The reduce() function starts with an initial value of 0 for the accumulated sum.
- The test_list variable initializes a list of tuples that will be used to test the sum_kth_column_reduce function.
- The K variable initializes the value of the index of the column to sum.
- The res variable initializes the sum to zero using the sum_kth_column_reduce function with test_list and K as arguments.
- The print() function is used to output the original list of tuples and the sum of the k-th column.
Below is the implementation of the above approach:
Python3
import functools
def sum_kth_column_reduce(tuples_list, k):
return functools. reduce ( lambda acc, tpl: acc + tpl[k], tuples_list, 0 )
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
K = 2
res = sum_kth_column_reduce(test_list, K)
print ( "The original list is : " + str (test_list))
print ( "Summation of Kth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31
Time complexity: O(n), where n is the length of the input tuples_list.
Auxiliary space: O(1), as the program only uses a constant amount of additional memory, regardless of the size of the input.
Similar Reads
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
Python | Summation of tuples in list
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. Metho
7 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 | 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 - Tuple Matrix Columns Summation
Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
8 min read
Python | Summation of list as tuple attribute
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the summation of list as tuple attribute. Let's discuss cert
9 min read
Python - Summation of kth column in a matrix
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the summation of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Us
8 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 - K Summation from Two lists
Sometimes, while working with Python lists we can have a problem in which we need to find summation of lists taking elements from two different lists. This kind of application can come in different domains such as web development. Lets discuss certain ways in which this task can be performed. Method
6 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