Sum all numbers in a given range of a given list Python

22,602

Solution 1

It seems like you want do roll your own solution. You can do it like this (based on the code you had in your question):

def sumRange(L,a,b):                                                                                                                                                                                                
    sum = 0                                                                                                                                                                                                         
    for i in range(a,b+1,1):                                                                                                                                                                                        
        sum += L[i]                                                                                                                                                                                                  
    return sum                                                                                                                                                                                                      

L = [6,3,4,2,5]                                                                                                                                                                                                     
a = 1                                                                                                                                                                                                               
b = 3                                                                                                                                                                                                               

result = sumRange(L,a,b)                                                                                                                                                                                            

print "The result is", result

This program prints

The result is 9

Solution 2

You can achieve this with list slicing:

sum(your_list[a:b + 1])

Here, your_list[a:b+1] is a slice - a part of your list starting from the index a and ending with the index b, including the values at both indexes (this is why you need b + 1).

Solution 3

You can simply use index slicing in python and the sum function.

return sum(L[a:b])
Share:
22,602
nightowl_nicky
Author by

nightowl_nicky

Updated on October 22, 2020

Comments

  • nightowl_nicky
    nightowl_nicky over 3 years

    How can I write a function to get the sum of the items in the given list between the indices a and b. For example give aList=[6,3,4,2,5] and a=1, b=3, the function should return 9. Here is my code:

    def sumRange(L,a,b):
        sum= []
        L = [6,3,4,2,5]
        for i in range(a,b+1,1):
        sum +=L[i]
        return sum