Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Column wise sum of nested list
A nested list in Python is a list that contains other lists as elements, creating a multidimensional structure. When working with nested lists representing tabular data, calculating column-wise sums is a common requirement.
In a nested list structure where each inner list represents a row, the elements at the same index across different inner lists form columns. Let's explore different methods to calculate column-wise sums.
Understanding Nested List Structure
Consider a nested list where each inner list represents a row of data:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Nested list:", nested_list)
print("Column 1:", [row[0] for row in nested_list])
print("Column 2:", [row[1] for row in nested_list])
print("Column 3:", [row[2] for row in nested_list])
Nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Column 1: [1, 4, 7] Column 2: [2, 5, 8] Column 3: [3, 6, 9]
Method 1: Using Nested Loops
The most straightforward approach uses nested loops to iterate through rows and columns:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
num_columns = len(nested_list[0])
column_sums = [0] * num_columns
for row in nested_list:
for i in range(num_columns):
column_sums[i] += row[i]
print("Column-wise sums:", column_sums)
Column-wise sums: [12, 15, 18]
Method 2: Using zip() with List Comprehension
The zip() function can transpose the nested list, making column-wise operations easier:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column_sums = [sum(column) for column in zip(*nested_list)]
print("Column-wise sums:", column_sums)
Column-wise sums: [12, 15, 18]
Method 3: Using NumPy
NumPy provides efficient array operations for mathematical computations:
import numpy as np
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(nested_list)
column_sums = np.sum(arr, axis=0)
print("Column-wise sums:", column_sums)
print("Data type:", type(column_sums))
Column-wise sums: [12 15 18] Data type: <class 'numpy.ndarray'>
Handling Irregular Nested Lists
For nested lists with different row lengths, we need special handling:
irregular_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
# Find maximum row length
max_cols = max(len(row) for row in irregular_list)
column_sums = [0] * max_cols
for row in irregular_list:
for i in range(len(row)):
column_sums[i] += row[i]
print("Column-wise sums for irregular list:", column_sums)
Column-wise sums for irregular list: [11, 14, 11, 9]
Performance Comparison
| Method | Readability | Performance | Memory Usage |
|---|---|---|---|
| Nested Loops | High | Moderate | Low |
| zip() + List Comprehension | High | Good | Moderate |
| NumPy | High | Excellent | Moderate |
Conclusion
Use zip() with list comprehension for readable and efficient code with regular nested lists. For large datasets or complex mathematical operations, NumPy provides the best performance. Nested loops offer the most control for irregular data structures.
