Sum all numbers in a given range of a given list Python
Asked Answered
A

3

7

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
Asta answered 14/6, 2016 at 15:52 Comment(0)
A
2

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

Androsterone answered 14/6, 2016 at 15:57 Comment(0)
P
13

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).

Philipines answered 14/6, 2016 at 15:54 Comment(0)
T
5

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

return sum(L[a:b])
Tice answered 14/6, 2016 at 15:54 Comment(0)
A
2

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

Androsterone answered 14/6, 2016 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.