# Python program to find Sum of a sublist

In this article, we will learn a python program to find the sum of a sublist.

### Methods Used

The following are the various methods to accomplish this task −

• Using For Loop(Brute Code)

• Using Cumulative Sum Method

• Using sum() Function

• Using math.fsum() Function

## Using For Loop (Brute Code)

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

• Create a variable to store the input list.

• Create two separate variables for storing the start and the end indices.

• Initialize a variable resultSum to 0 for storing the resultant sum of a sublist.

• Use the for loop, to traverse the range from the given start index to the end index.

• Add the corresponding value at the iterator index to the above-defined resultSum variable(sum of elements from the given start and end indices)

• Print the resultant sum of the sublist(from start to end index).

### Example

The following program returns the sum of sublist i.e, sum of elements from the given start and end indices using the for loop −

# input list
inputList = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", inputList)
# starting index
start_index = 1
# ending index
end_index = 5
# initializing a variable to 0 for storing the resultant sublist sum
resultSum = 0
# traversing in the range from the given start index to the end index
for k in range(start_index, end_index+1):
# adding corresponding value at the iterator index to the resultSum variable
resultSum += inputList[k]
# Printing the resultant sum of sublist(from start to end index)
print("The resultant sum of sublist is:", resultSum)


### Output

On executing, the above program will generate the following output −

The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]
The resultant sum of sublist is: 25


## Using Cumulative Sum Method

The previous element values are added to the current index value using the cumulative sum method.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

• Use the for loop, to loop till the length of the input list with the len() function(returns the number of items in an object).

• If the current index is 0 then there will be no element at the previous index so continue the iteration using the continue statement.

• Else add the value of the previous element to the current element(Cummulative Sum).

• Use the if conditional statement to check whether the given start index is 0.

• Print the element at the given end index of the input list, if the above if condition is true.

• Else print the difference of elements at the given end index and the previous of start index.

### Example

The following program returns the sum of the sublist i.e, the sum of elements from the given start and end indices using the cumulative sum method −

# input list
inputList = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", inputList)
# starting index
start_index = 1
# ending index
end_index = 5
# looping till the length of the input list
for k in range(len(inputList)):
# If it the index is 0 i.e first element then continue(do nothing)
if(k == 0):
continue
else:
# Else add the previous element value to the current element
inputList[k] = inputList[k]+inputList[k-1]
print("The resultant sum of sublist is:")
# checking whether the given starting index is 0
if(start_index == 0):
# printing the element at the given end index of the list
print(inputList[end_index])
else:
# Else printing the difference of elements at the given end index
# and previous of start index
print(inputList[end_index]-inputList[start_index-1])


### Output

The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]
The resultant sum of sublist is:
25


## Using sum() Function

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

• Get the list elements from the start index to end index using slicing.

• Use the sum() function(returns the sum of all items in any iterable) to print the sum of sublist i.e, the sum of elements from the given start index to the end index.

### Example

The following program returns the sum of the sublist i.e, the sum of elements from the given start and end indices using sum() function −

# input list
inputList = [3, 5, 10, 5, 2, 3, 1, 20]
print("The Given List is:", inputList)
start_index = 1
end_index = 5
print("The resultant sum of sublist is:")
# Getting the list elements between start and end indices
resultList = inputList[start_index:end_index+1]
# Printing the sum of the sublist i.e printing above resultList sum
print(sum(resultList))


### Output

The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]
The resultant sum of sublist is:
25


## Using math.fsum() Function

fsum() is one of the special functions in the math module. The sum of the sub-list can then be computed using the fsum() function.

The math.fsum() function in python, returns the sum of all items in any iterable like tuples, arrays, lists, etc.

### Example

The following program returns the sum of the sublist i.e, the sum of elements from the given start and end indices using math.fsum() function −

# importing math module
import math
# input list
inputList = [3, 5, 10, 5, 2, 3, 1, 20]
# starting index
start_index = 1
# ending index
end_index = 5
print("The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]")
print("The resultant sum of sublist is:")
# Getting the list elements between start and end indices
resultList = inputList[start_index:end_index+1]
# Printing the sum of the sublist i.e printing above resultList sum
print(math.fsum(resultList))


### Output

The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]
The resultant sum of sublist is:
25.0


## Conclusion

We learned how to find the sum of a sublist, i.e. the sum between the given start and end indices, using four distinct methods in this article. We also learned how to use slicing to get a part of a list.

Updated on: 31-Jan-2023

2K+ Views