VOOZH about

URL: https://www.geeksforgeeks.org/python/python-summation-of-kth-column-of-tuple-list/

⇱ Python | Summation of Kth Column of Tuple List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Summation of Kth Column of Tuple List

Last Updated : 6 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. 


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. 


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.

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

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:

  1. Define a recursive function sum_kth_column_recursive that takes two arguments: tuples_list and k.
  2. If tuples_list is empty, return 0 (base case).
  3. 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).
  4. Return the final sum.

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:


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.

Comment