Python - Column wise sum of nested list


A nested list in Python is a list that contains other lists as elements. It is a way to create a hierarchical or multidimensional structure within a single list. The inner lists can themselves contain any type of elements, including other lists.

Nested lists are useful when dealing with multidimensional data, such as matrices, tables, or hierarchical structures. They provide a flexible and convenient way to organize and access data in a structured manner. Let’s create a nested list as an example.

Example

In this example, we are having a nested list with three inner lists. Each inner list represents a row, and the elements within each inner list represent the values in different columns.

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("The nested list:",nested_list)

Output

The nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The structure of a nested list allows for more complex data representations also. For instance, we can have different lengths for the inner lists, creating a jagged or uneven structure below.

Example

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print("The nested list with different sizes:",nested_list)

Output

The nested list with different sizes: [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

In python to calculate the column-wise sum of a nested list, we can use different approaches. Here are the detailed explanations for each approach with an example.

Using Loops

If we have a nested list where each inner list represents a row, and the elements within the inner list represent the values in different columns, we can use a loop to iterate through the columns and calculate the sum for each column.

Example

In this example, we first calculate the number of columns `num_columns` by accessing the length of the first inner list. We initialize a list `column_sum` with zeroes to store the sum for each column. Then, we iterate through each row in the nested list, and for each row, we iterate through the indices of the elements in the row. We update the corresponding element in `column_sum` by adding the value from the current row.

nested_list = [[1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]]
num_columns = len(nested_list[0])
column_sum = [0] * num_columns
for row in nested_list:
   for i in range(num_columns):
      column_sum[i] += row[i]
print("The column sum of each of the nested lists:",column_sum)

Output

The column sum of each of the nested lists: [12, 15, 18]

Using `zip()` and a List Comprehension

We can leverage the zip() function to transpose the nested list, grouping the values of each column together. Then, we can use a list comprehension to calculate the sum for each column.

Example

In this example, zip(*nested_list) transposes the nested list, creating an iterator that returns tuples with the elements from each column. The list comprehension then calculates the sum of each column using sum(column), resulting in a list of column sums.

nested_list = [[1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]]
column_sum = [sum(column) for column in zip(*nested_list)]
print("The column sum of each of the nested lists:",column_sum)

Output

The column sum of each of the nested lists: [12, 15, 18]

Using NumPy

Numpy is one of the most useful libraries for computing the scientific and mathematical calculations using python. We have a function namely sum() in numpy library which helps in calculating the column wise sum of the nested lists.

Example

In this example, we are converting the nested list into a NumPy array by using np.array(nested_list), where each inner list represents a row in the array. Then, we use np.sum(arr, axis=0) to calculate the sum along the first axis i.e. rows, effectively giving us the column-wise sums.

import numpy as np
nested_list = [[1, 2, 3],
   [4, 5, 6],
   [7, 8, 10]]
arr = np.array(nested_list)
column_sum = np.sum(arr, axis=0)
print("The column sum of each of the nested lists:",column_sum)

Output

The column sum of each of the nested lists: [12 15 19]

Updated on: 02-Jan-2024

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements