How do you sum a range in a list in python?

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

How do you sum a range in a list in python?

asked Jun 14, 2016 at 15:52

nightowl_nickynightowl_nicky

3211 gold badge4 silver badges13 bronze badges

0

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

answered Jun 14, 2016 at 15:54

How do you sum a range in a list in python?

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

return sum(L[a:b])

answered Jun 14, 2016 at 15:54

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

answered Jun 14, 2016 at 15:57

In this short tutorial, we look at how we can use Python to find the sum() of a list. We look at the various methods to do this along with their limitations.

Table of Contents - Python Sum List:

  1. Using Sum to find the sum of a List
  2. How to use the sum() function?
  3. Limitation and Caverts - Python Sum List

Python Sum List:

While using Python, there are sure to be numerous use cases where you might have to calculate the sum of an iterable. For the purpose of this blog, we mainly focus on lists; however, the same method can be applied to other iterables as well.

An example of a use case is the use of sum() to return the sum of a list that contains the weekly income of employees to calculate monthly income.

How to use the sum() function?

The sum() function returns the sum of an iterable. Sum() takes a list (iterable) and returns the sum of the numbers within the list.

The syntax is as follows:

sum(iterable, start) 

Parameters:

  1. Iterable - Required, iterable can be a list, tuples, and dictionary
  2. Start - Optional, if passed it the value will be added returned sum

Code and Explanation:

#Using range to create list of numbers
numbers = list(range(0,10))

sum_numbers = sum(numbers)
print(sum_numbers)
#Output - 45

#Passing an argument as start
sum_numbers = sum(numbers, 10)
print(sum_numbers)
#Output - 55

As seen in the above code snippet, the sum() function is used to add the values in the range that has been specified. You can similarly use the function for various operations.

Limitations and Caveats - Python Sum List

A common error that arises while using the sum() function, is when the list contains a string. Since it is not possible to add int values in strings, Python returns a TypeError. Let us look at such an instance.

#Creating a list of number and a string
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "10"]

sum_numbers = sum(numbers)
print(sum_numbers)

Python returns this output:

Traceback (most recent call last):
  File "C:\Users\Python\Using_sum.py", line 4, in 
    sum_numbers = sum(numbers)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

As explained, the int value in the string causes the TypeError. Other than this limitation, you can make use of the sum() function in Python with ease for all summing operations.

How do you sum items in a list in Python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

How do you find the sum of the range?

Here's a formula that uses two cell ranges: =SUM(A2:A4,C2:C3) sums the numbers in ranges A2:A4 and C2:C3. You'd press Enter to get the total of 39787.

Can you sum a list in Python?

Python's built-in function sum() is an efficient and Pythonic way to sum a list of numeric values.

How do you write a function to sum all numbers in a list Python?

Python function to sum all the numbers in a list example.
Using sum() method. ... .
Using while() loop total = 0 ele = 0 list1 = [11, 5, 17, 18, 23] while ele < len(list1): total = total + list1[ele] ele += 1 print(total).