![]() |
VOOZH | about |
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.
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.
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)
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.
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.
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.
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).
Algorithm:
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.
Step-by-step approach:
Below is the implementation of the above approach:
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.