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 certain ways in which this task can be performed.
Method #1: Using list comprehension + sum() This particular problem can be solved using list comprehension combined with the sum function in which we use sum function to find the sum of list as a tuple attribute and list comprehension to iterate through the list.
Python3
# Python3 code to demonstrate # Summation of list as tuple attribute # using list comprehension + sum() # initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # printing original list print ("The original list : " + str (test_list)) # using list comprehension + sum() # Summation of list as tuple attribute res = [(key, sum (lst)) for key, lst in test_list] # print result print ("The list tuple attribute summation is : " + str (res)) |
The original list : [(‘key1’, [3, 4, 5]), (‘key2’, [1, 4, 2]), (‘key3’, [9, 3])] The list tuple attribute summation is : [(‘key1’, 12), (‘key2’, 7), (‘key3’, 12)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, as the output list created by list comprehension will have the same number of elements as the input list. Additionally, the space required to store the input list and other program variables is negligible compared to the size of the input list.
Method #2: Using map + lambda + sum() The above problem can also be solved using the map function to extend the logic to the whole list and sum function can perform the similar task as the above method.
Python3
# Python3 code to demonstrate # Summation of list as tuple attribute # using map() + lambda + sum() # initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # printing original list print ("The original list : " + str (test_list)) # using map() + lambda + sum() # Summation of list as tuple attribute res = list ( map ( lambda x: (x[ 0 ], sum (x[ 1 ])), test_list)) # print result print ("The list tuple attribute summation is : " + str (res)) |
The original list : [(‘key1’, [3, 4, 5]), (‘key2’, [1, 4, 2]), (‘key3’, [9, 3])] The list tuple attribute summation is : [(‘key1’, 12), (‘key2’, 7), (‘key3’, 12)]
Time complexity: The time complexity of the given code is O(n), where n is the length of the input list.
Auxiliary space: The auxiliary space used by the given code is O(n), where n is the length of the input list.
Method #3: Using starmap
First, the itertools module is imported to make the starmap function available. Then, the list of tuples is initialized and printed to show the original data.
Next, the starmap function is used to apply the sum function to the list attribute of each tuple in the list. The starmap function expects a function as its first argument, and then a list of tuples as its second argument. In this case, the lambda function is used as the first argument, and it takes two arguments: x and y. The lambda function returns a tuple containing the first element of the input tuple (x) and the sum of the second element (y). The starmap function applies this lambda function to each tuple in the input list, and returns an iterator of the results. This iterator is then converted to a list using the built-in list function.
Python3
from itertools import starmap # Initialize the list of tuples test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # Print the original list print ( "The original list:" , test_list) # Use starmap to apply the sum function to the list attribute of each tuple res = list (starmap( lambda x, y: (x, sum (y)), test_list)) # Print the result print ( "The list tuple attribute summation:" , res) #This code is contributed by Edula Vinay Kumar Reddy |
The original list: [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute summation: [('key1', 12), ('key2', 7), ('key3', 12)]
Time complexity: O(n) , n is number of elements in all list of tuples
Auxiliary Space: O(n)
Method #4: Using a for loop
Use a for loop to iterate over the list of tuples. For each tuple, it computes the sum of the list using the sum() function and creates a new tuple with the key and the sum as the elements. This tuple is then appended to a new list, which is the final result.
Python3
# initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # printing original list print ( "The original list : " + str (test_list)) # using for loop # Summation of list as tuple attribute res = [] for key, lst in test_list: total = sum (lst) res.append((key, total)) # print result print ( "The list tuple attribute summation is : " + str (res)) |
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute summation is : [('key1', 12), ('key2', 7), ('key3', 12)]
Time complexity: O(n), n is number of elements in all list of tuples
Auxiliary Space: O(n)
Method #5: Using a generator expression + sum()
In this implementation, we create a generator expression instead of a list comprehension by wrapping the expression in parentheses instead of brackets. The generator expression is then passed to the list() function to create a list of the results.
Python3
# Python3 code to demonstrate # Summation of list as tuple attribute # using generator expression + sum() # initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # printing original list print ( "The original list : " + str (test_list)) # using generator expression + sum() # Summation of list as tuple attribute res = ((key, sum (lst)) for key, lst in test_list) # print result print ( "The list tuple attribute summation is : " + str ( list (res))) |
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute summation is : [('key1', 12), ('key2', 7), ('key3', 12)]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1), as we are not using any additional data structures that depend on the size of the input list.
Method#6: Using Recursive method.
This implementation uses recursion to iterate through each tuple in the test_list. The base case is when i is greater than or equal to the length of test_list. If i is less than the length of test_list, it calculates the sum of the second element of the tuple and creates a new tuple with the same key and the sum as the value. It then concatenates this tuple with the result of the recursive call with i incremented by 1. The final result is returned as a list of tuples.
Python3
def tuple_attribute_sum_recursive(test_list, i = 0 ): if i > = len (test_list): return [] else : total = sum (test_list[i][ 1 ]) return [(test_list[i][ 0 ], total)] + tuple_attribute_sum_recursive(test_list, i + 1 ) # initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] # printing original list print ( "The original list : " + str (test_list)) # using recursive function # Summation of list as tuple attribute res = tuple_attribute_sum_recursive(test_list) # print result print ( "The list tuple attribute summation is : " + str (res)) |
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute summation is : [('key1', 12), ('key2', 7), ('key3', 12)]
The time complexity of the recursive implementation of tuple_attribute_sum_recursive function is O(n), where n is the length of the test_list. This is because we visit each element of the test_list exactly once, and for each element, we do a constant amount of work, including a recursive call with the index incremented by 1.
The space complexity of the recursive implementation is also O(n), where n is the length of the test_list. This is because at any point during the recursion, there will be at most n recursive calls on the call stack, each of which stores a constant amount of information, including the current index and the res list.
Method #4: Using numpy.sum()
This method uses numpy to sum the list in each tuple of the list. The numpy.sum() function is used to sum the array of each tuple.
Note: numpy.sum() can only sum 1D and 2D arrays, so we need to flatten the list inside the tuple first.
Python3
import numpy as np #initializing list test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])] #printing original list print ( "The original list : " + str (test_list)) #using numpy.sum() to sum the list inside each tuple #Summation of list as tuple attribute res = [(key, np. sum (np.array(lst).flatten())) for key, lst in test_list] #print result print ( "The list tuple attribute summation is : " + str (res)) |
Output:
The original list : [(‘key1’, [3, 4, 5]), (‘key2’, [1, 4, 2]), (‘key3’, [9, 3])]
The list tuple attribute summation is : [(‘key1’, 12), (‘key2’, 7), (‘key3’, 12)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, as the output list created by list comprehension will have the same number of elements as the input list. Additionally, the space required to store the input list, numpy array, and other program variables is negligible compared to the size of the input list.