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 program to find Sum of a sublist
In this article, we will learn different methods to find the sum of a sublist in Python. A sublist is a contiguous portion of a list between specified start and end indices.
Methods Used
The following are the various methods to accomplish this task ?
Using For Loop (Brute Force)
Using Cumulative Sum Method
Using sum() Function
Using math.fsum() Function
Using For Loop (Brute Force)
This method iterates through the sublist elements and adds them one by one ?
# Input list
numbers = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", numbers)
# Starting and ending indices
start_index = 1
end_index = 5
# Initialize sum variable
result_sum = 0
# Traverse from start index to end index
for k in range(start_index, end_index + 1):
result_sum += numbers[k]
print("The resultant sum of sublist is:", result_sum)
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
Using Cumulative Sum Method
This method creates a cumulative sum array where each element contains the sum of all previous elements. Then we use the difference to get the sublist sum ?
# Input list
numbers = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", numbers)
# Starting and ending indices
start_index = 1
end_index = 5
# Create cumulative sum
cumulative = numbers.copy() # Don't modify original list
for k in range(1, len(cumulative)):
cumulative[k] = cumulative[k] + cumulative[k-1]
# Calculate sublist sum using cumulative array
if start_index == 0:
result_sum = cumulative[end_index]
else:
result_sum = cumulative[end_index] - cumulative[start_index-1]
print("The resultant sum of sublist is:", result_sum)
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
Using sum() Function
The most Pythonic approach uses list slicing with the built-in sum() function ?
# Input list
numbers = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", numbers)
start_index = 1
end_index = 5
# Get sublist using slicing and calculate sum
sublist = numbers[start_index:end_index + 1]
result_sum = sum(sublist)
print("The resultant sum of sublist is:", result_sum)
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
Using math.fsum() Function
The math.fsum() function provides more accurate floating-point summation compared to the built-in sum() function ?
import math
# Input list
numbers = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", numbers)
start_index = 1
end_index = 5
# Get sublist and calculate sum using math.fsum()
sublist = numbers[start_index:end_index + 1]
result_sum = math.fsum(sublist)
print("The resultant sum of sublist is:", result_sum)
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25.0
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| For Loop | O(n) | Understanding basic logic |
| Cumulative Sum | O(n) | Multiple sublist queries |
| sum() Function | O(n) | Simple and readable code |
| math.fsum() | O(n) | High precision floating-point |
Conclusion
Use sum() with list slicing for most cases as it's the most readable. Use math.fsum() when working with floating-point numbers that require high precision. The cumulative sum method is efficient when you need to calculate multiple sublist sums from the same array.
