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. Let’s discuss certain ways in which this problem can be performed in error-free manner.
Method #1 : Using sum() + filter() + map() + list comprehension The combination of above three function combined with list comprehension can help us perform this particular task, the sum function helps to perform the summation, filter allows us to check for the present elements and all rows are combined using the map function. Works only with python 2
Python3
test_list = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
print ("The original list is : " + str (test_list))
res = [ sum ( filter ( None , j)) for j in map ( None , * test_list)]
print ("The summation of columns is : " + str (res))
|
Output : The original list is : [[1, 5, 3], [4], [9, 8]]
The summation of columns is : [14, 13, 3]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using list comprehension + sum() + zip_longest() If one desires not to play with the None values, one can opt for this method to resolve this particular problem. The zip_longest function helps to fill the not present column with 0 so that it does not has to handle the void of elements not present.
Python3
import itertools
test_list = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
print ("The original list is : " + str (test_list))
res = [ sum (i) for i in itertools.zip_longest( * test_list, fillvalue = 0 )]
print ("The summation of columns is : " + str (res))
|
Output : The original list is : [[1, 5, 3], [4], [9, 8]]
The summation of columns is : [14, 13, 3]
Method #3 : Using max
To perform column summation in uneven sized lists in Python, you can use the built-in zip() function to group the elements of each sub-list by their corresponding positions, and then use a list comprehension to sum the elements in each group.
Python3
original_list = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
max_length = max ( len (sub_list) for sub_list in original_list)
column_sums = [ 0 ] * max_length
for sub_list in original_list:
for i in range ( len (sub_list)):
column_sums[i] + = sub_list[i]
print ( "The original list is :" , original_list)
print ( "The summation of columns is :" , column_sums)
|
OutputThe original list is : [[1, 5, 3], [4], [9, 8]]
The summation of columns is : [14, 13, 3]
Time complexity: O(M*N)
Auxiliary Space: O(M)
Method #4 : Using reduce() and zip_longest():
Algorithm:
1.Initialize the list of lists.
2.Use itertools.zip_longest() to make lists equal in size by adding 0s to fill the missing values.
3.Use list comprehension to sum the columns.
4.Return the resulting list as the column summation of the uneven sized lists.
Python3
from functools import reduce
import itertools
test_list = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
print ( "The original list is:" , test_list)
res = list ( reduce ( lambda x, y: [a + b for a, b in itertools.zip_longest(x, y, fillvalue = 0 )], test_list))
print ( "The summation of columns is:" , res)
|
OutputThe original list is: [[1, 5, 3], [4], [9, 8]]
The summation of columns is: [14, 13, 3]
Time complexity: O(n*m) where n is the number of sublists and m is the maximum length of sublists. This is because we are iterating over each element in each sublist to compute the summation.
Space complexity: O(m) where m is the maximum length of sublists. This is because we are creating a new list to store the summation of columns, which has length equal to the maximum length of sublists. Additionally, we are using itertools.zip_longest to iterate over the sublists, which also requires memory proportional to the length of the longest sublist.
METHOD 5:Using .items
APPROACH:
The approach used here is to first create an empty dictionary result to keep track of the column-wise sum. Then for each sublist in the input list, it iterates over the elements in the sublist using the enumerate() function. For each element, it adds the value to the corresponding key in the result dictionary. If the key is not present in the dictionary, it initializes it with a default value of 0. Finally, it sorts the dictionary by key and creates a list of values in the sorted order.
ALGORITHM:
1.Initialize an empty dictionary result.
2.Loop through each sublist in the input list and each element in the sublist:
a. If the dictionary already has a key for the current index, add the element to the value associated with that key.
b. Otherwise, add a new key-value pair to the dictionary with the current index as the key and the element as the value.
3.Convert the dictionary to a list of tuples and sort it by the keys.
4.Extract the values from the sorted list of tuples to get the column sums.
5.Return the column sums.
Python3
def column_sum(lists):
result = {}
for sublist in lists:
for i, val in enumerate (sublist):
result[i] = result.get(i, 0 ) + val
sorted_result = sorted (result.items())
return [val for key, val in sorted_result]
lists = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
result = column_sum(lists)
print (result)
|
Time complexity: The algorithm has a time complexity of O(n * m), where n is the number of sublists in the input list and m is the length of the longest sublist.
Auxiliary Space: The algorithm has a space complexity of O(m), because it creates a dictionary with at most m keys and values.
Approach using numpy:
Note: Install numpy module using command “pip install numpy”
Algorithm:
Initialize a numpy array from the given list of lists.
Calculate the sum of the columns using the sum() function on axis 0.
Convert the resulting numpy array back to a list.
Python3
import numpy as np
test_list = [[ 1 , 5 , 3 ], [ 4 ], [ 9 , 8 ]]
print ( "The original list is:" , test_list)
arr = np.array([np.pad(row, ( 0 , len ( max (test_list, key = len )) - len (row)), 'constant' ) for row in test_list])
res = np. sum (arr, axis = 0 ).tolist()
print ( "The summation of columns is:" , res)
|
Output:
The original list is: [[1, 5, 3], [4], [9, 8]]
The summation of columns is: [14, 13, 3]
Time complexity: O(n^2) for creating the numpy array, where n is the length of the longest sub-list in the given list of lists.
Auxiliary Space: O(n^2) for the numpy array created, where n is the length of the longest sub-list in the given list of lists.