Open In App

Find sum and average of List in Python

Given a List. The task is to find the sum and average of the list. The average of the list is defined as the sum of the elements divided by the number of elements.

Examples:



Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
sum =  46
average =  5.75

Input: [15, 9, 55, 41, 35, 20, 62, 49]
Output:
sum =  286
average =  35.75

Method 1: Naive method

In this method, we will iterate over the list of and will add each element to a variable count which stores the sum of the ith element and then dividing the sum with the total number of variables to find the average.



Example:




# Python program to find the sum
# and average of the list
 
L = [4, 5, 1, 2, 9, 7, 10, 8]
 
 
# variable to store the sum of
# the list
count = 0
 
# Finding the sum
for i in L:
    count += i
 
# divide the total elements by
# number of elements
avg = count/len(L)
 
print("sum = ", count)
print("average = ", avg)

Output
sum =  46
average =  5.75

Time complexity: O(n)
Auxiliary Space: O(n), where n is length of list

Method 2: Using sum() method

sum() method returns the sum of the list passed as its argument. Then we will divide the sum by the len() method to find the average.

Example:




# Python program to find the sum
# and average of the list
 
L = [4, 5, 1, 2, 9, 7, 10, 8]
 
 
# using sum() method
count = sum(L)
 
# finding average
avg = count/len(L)
 
print("sum = ", count)
print("average = ", avg)

Output
sum =  46
average =  5.75

Time Complexity: O(n) where n is the number of elements in the list
Auxiliary Space: O(1), constant extra space is required

Method 3 : Using sum() and statistics.mean()




# Python program to find the sum
# and average of the list
 
import statistics
L = [4, 5, 1, 2, 9, 7, 10, 8]
 
 
# using sum() method
sum1 = sum(L)
 
# finding average
avg = statistics.mean(L)
 
print("sum = ", sum1)
print("average = ", avg)

Output
sum =  46
average =  5.75

method 4: using NumPy module functions sum() and average()




import numpy as np
 
L = [4, 5, 1, 2, 9, 7, 10, 8]
 
# finding sum of list using numpy
sum_ = np.sum(L)
 
# finding average of list using numpy
avg = np.average(L)
 
print("sum = ", sum_)
print("average = ", avg)

Output

sum =  46
average =  5.75

Time complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using Recursion

The step-by-step approach of the program:




def sum_avg_list(lst, n):
    if n == 0:
        return (0, 0)
    else:
        sum, avg = sum_avg_list(lst, n-1)
        return (sum + lst[n-1], avg + 1)
 
def avg_list(lst):
    sum, avg = sum_avg_list(lst, len(lst))
    return sum/avg
 
lst = [4, 5, 1, 2, 9, 7, 10, 8]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst))
 
lst = [15, 9, 55, 41, 35, 20, 62, 49]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst))

Output
Sum of the list:  46
Average of the list:  5.75
Sum of the list:  286
Average of the list:  35.75

Time complexity: O(n) where n is the length of the list. 
Auxiliary space: O(n)

Method 6: using the built-in functions reduce() and lambda
Import the reduce function from the functools module. This function is used to reduce a sequence of values to a single value by applying a given function to each element in the sequence.
Define a lambda function that takes two arguments x and y and returns their sum x + y. This will be used by the reduce function to sum the elements of the list.
Use the reduce function with the lambda function and the list L as arguments to obtain the sum of the elements in the list. Store this value in a variable count.
Divide the value of count by the length of the list L using the len() function to obtain the average value. Store this value in a variable avg.
Print the value of count and avg.




from functools import reduce
 
L = [4, 5, 1, 2, 9, 7, 10, 8]
 
# Finding the sum using reduce() and lambda
count = reduce(lambda x, y: x + y, L)
 
# divide the total elements by number of elements
avg = count/len(L)
 
print("sum = ", count)
print("average = ", avg)

Output
sum =  46
average =  5.75

Time complexity: O(n) for iterating through the list once, and O(n) for reducing the list to a single value, resulting in O(n) overall.

Auxiliary space: O(1) as we are only using a constant amount of extra space to store the variables count and avg.

Method 7: using only the built-in functions sum() and len():




L = [4, 5, 1, 2, 9, 7, 10, 8]
 
# Finding the sum
count = sum(L)
 
# Finding the average
avg = count/len(L)
 
print("sum = ", count)
print("average = ", avg)

Output
sum =  46
average =  5.75

Time complexity: O(n), where n is the length of the list L. Both sum() and len() functions take O(n) time in the worst case.

Auxiliary space: O(1), since we only use a constant amount of extra space for the count and avg variables.


Article Tags :