![]() |
VOOZH | about |
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 changes.
Method 1: Using Counter() + "+" operator
This task can be performed using the Counter function as it internally groups and an addition operator can be used to specify the functionality of the grouped result.
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key2', 9), ('key1', 8), ('key3', 8)]Time complexity: O(n), where n is the length of the input lists.
Auxiliary Space: O(m), where m is the number of unique keys in the input lists.
Method 2: Using for loop+ "+" operator
This approach uses a dictionary to store the keys and values of the tuples. It iterates over the tuples in the list, adding the values to the corresponding keys in the dictionary. Finally, it converts the dictionary to a list of tuples.
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key1', 8), ('key3', 8), ('key2', 9)]Time complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using defaultdict and a for loop
The grouped summation tuple list is : [('key1', 8), ('key3', 8), ('key2', 9)]Time complexity: O(n), where n is the total number of tuples in both lists. The for loop iterates over each tuple once, and the time to update the defaultdict with a new key-value pair is O(1).
Auxiliary space: O(n). We use a defaultdict object named dict_sum to store the intermediate results.
Method 4: Using itertools.groupby() and a list comprehension
This code first merges the two input lists into a single list, and then sorts the list by key using a lambda function. The groupby function from itertools is then used to group the tuples by key, and a list comprehension is used to calculate the sum of values for each group. Finally, the result is printed.
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]Time complexity: O(N log N), where N is the total number of tuples in the input lists.
Auxiliary space: O(N)
Method 5: Using a dictionary comprehension.
Algorithm:
The grouped summation tuple list is : [('key2', 9), ('key1', 8), ('key3', 8)]Time Complexity: O(n), where n is the total number of tuples in both lists. This is because the list comprehension and dictionary comprehension both iterate through the combined list of tuples, which takes linear time.
Auxiliary Space: O(n), where n is the total number of tuples in both lists. This is because the resulting dictionary will have at most one key-value pair for each unique key in the combined list of tuples, which can take up to n space. Additionally, the resulting list of tuples will also take up to n space.
Method 6 : using pandas library.
step by step approach:
OUTPUT:
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]
Time complexity: O(n log n), where n is the total number of elements in both lists. This is because the concatenation operation and the grouping operation both require sorting, which has a time complexity of O(n log n).
Auxiliary space: O(n), where n is the total number of elements in both lists. This is because the dataframes and the resulting dictionary both require memory proportional to the number of elements in the input lists.
Method 7: Using heapq:
Algorithm:
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]Time complexity:
The time complexity of the algorithm is O(n log n), where n is the total number of tuples in the input lists. This is due to the initial sorting of the merged list and the iteration over each group of tuples.
Space complexity:
The space complexity of the algorithm is O(n), where n is the total number of tuples in the input lists. This is due to the storage of the merged list and the grouped list in memory. The groupby() function does not create additional copies of the input data, so it does not contribute to the space complexity.
Method 8: Using reduce():
Algorithm:
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]Time Complexity:
The time complexity of this solution is O(nlogn) because of the sorting operation. The reduce function operation is O(n).
Space Complexity:
The space complexity of this solution is O(n) because we are creating a merged_list and the final output list, both of which can have a maximum of n elements. In addition, there is a small constant space used by the variables for the reduce function.