How do you sum parts of a list in python?

weekly = [ sum(visitors[x:x+7]) for x in range(0, len(daily), 7)]

Or slightly less densely:

weekly = []
for x in range(0, len(daily), 7):
     weekly.append( sum(visitors[x:x+7]) )

Alternatively, using the numpy module.

by_week = numpy.reshape(visitors, (7, -1))
weekly = numpy.sum( by_week, axis = 1)

Note that this requires the number of elements in visitor be a multiple of 7. It also requires that you install numpy. However, its probably also more efficient then the other approaches.

Or for itertools code bonus:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

weekly = map(sum, grouper(7, visitors, 0))

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sum of numbers in the list is required everywhere. 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. 
    If start is not given in the syntax , it is assumed to be 0.

    Possible two syntaxes:

    sum(a)
    a is the list , it adds up all the numbers in the 
    list a and takes start to be 0, so returning 
    only the sum of the numbers in the list.
    sum(a, start)
    this returns the sum of the list + start 

    Below is the Python implementation of the sum() 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum(numbers)

    print(Sum)

    Sum = sum(numbers, 10)

    print(Sum)

    Output:

    25
    35

    Error and Exceptions

    TypeError : This error is raised in the case when there is anything other than numbers in the list. 

    Python3

    arr = ["a"]

    Sum = sum(arr)

    print(Sum)

    Sum = sum(arr, 10)

    print(Sum)

    Runtime Error :

    Traceback (most recent call last):
      File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in 
        Sum = sum(arr)
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    How do you sum parts of a list in python?
    So the list should contain numbers Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers. 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum(numbers)

    average= Sum/len(numbers)

    print (average)

    Output:

    3

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list of numbers, write a Python program to find the sum of all the elements in the list.

    Example:  

    Input: [12, 15, 3, 10]
    Output: 40
    Input: [17, 5, 3, 5]
    Output: 30

    Example #1: 

    Python3

    total = 0

    list1 = [11, 5, 17, 18, 23]

    for ele in range(0, len(list1)):

        total = total + list1[ele]

    print("Sum of all elements in given list: ", total)

    Output

    Sum of all elements in given list:  74

    Example #2 : Using while() loop  

    Python3

    total = 0

    ele = 0

    list1 = [11, 5, 17, 18, 23]

    while(ele < len(list1)):

        total = total + list1[ele]

        ele += 1

    print("Sum of all elements in given list: ", total)

    Output: 
     

    Sum of all elements in given list:  74

    Example #3: Recursive way  

    Python3

    list1 = [11, 5, 17, 18, 23]

    def sumOfList(list, size):

        if (size == 0):

            return 0

        else:

            return list[size - 1] + sumOfList(list, size - 1)

    total = sumOfList(list1, len(list1))

    print("Sum of all elements in given list: ", total)

    Output

    Sum of all elements in given list:  74

    Example #4: Using sum() method  

    Python3

    list1 = [11, 5, 17, 18, 23]

    total = sum(list1)

    print("Sum of all elements in given list: ", total)

    Output: 

    Sum of all elements in given list:  74

    Example 5: Using add() function of operator module

    First we have to import the operator module then using the add() function of operator module adding the all values in the list. 

    Python3

    from operator import*

    list1 = [12, 15, 3, 10]

    result = 0

    for i in list1:

        result = add(i, 0)+result

    print(result)

    Method 6: Using enumerate function

    Python3

    list1 = [12, 15, 3, 10];s=0

    for i,a in enumerate(list1):

      s+=a

    print(s)

    Method 7: Using list comprehension 

    Python3

    list1 = [12, 15, 3, 10]

    s=[i for i in list1]

    print(sum(s))

    Method 8: Using lambda function

    Python3

    list1 = [12, 15, 3, 10]

    print(sum(list(filter(lambda x: (x),list1))))

    Method: Using add operator 

    Python3

    import operator

    list1 = [12, 15, 3, 10] ;s=0

    for i in list1:

      s=s+operator.add(0,i)

    print(s)


    How do you sum the components of 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 a list of elements?

    Algorithms.
    def sum_of_list(l): total = 0. for val in l: total = total + val. return total. ​ my_list = [1,3,5,2,4] ... .
    def sum_of_list(l,n): if n == 0: return l[n]; return l[n] + sum_of_list(l,n-1) ​ my_list = [1,3,5,2,4] ... .
    my_list = [1,3,5,2,4] print "The sum of my_list is", sum(my_list) Run..

    How do I get part of a list in Python?

    How to Get Specific Elements From a List?.
    Get elements by index. use the operator [] with the element's index. use the list's method pop(index) use slicing lst[start:stop:step] to get several elements at once. ... .
    Get elements by condition. use the filter() function. use a list comprehension statement..