Thursday, December 26, 2024
Google search engine
HomeLanguagesFind sum and average of List in Python

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:

Python3




# 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:

Python3




# 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()

Python3




# 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()

Python3




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:

  • Define a recursive function sum_avg_list with parameters lst and n that takes a list and its length as input.
    • If the length n of the list is 0, return (0, 0) to indicate that the sum and average of the list is 0.
    • If n is not 0, call the sum_avg_list function recursively with lst and n-1 as inputs. This will calculate the sum and average of the elements from 0 to n-1 in the list.
    • Store the result of the recursive call in the variables sum and avg.
    • Add the n-1th element of the list to the sum.
    • Increment the avg by 1.
    • Return the sum and avg.
  • Define another function avg_list that takes a list as input and calls the sum_avg_list function to calculate the sum and average.
    • Store the result of the sum_avg_list function in variables sum and avg.
    • Divide sum by avg and return the result as the average of the list.
    • Call the avg_list function with two example lists and print the results.
  • The program outputs the sum and average of the two lists.

Python3




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.

Python3




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():

  • Create a list L with the given elements.
  • Use the built-in function sum() to find the sum of all the elements in the list L and store it in a variable named count.
  • Use the built-in function len() to find the number of elements in the list L.
  • Divide the value of count by the length of the list L to find the average of the elements in the list L and store it in a variable named avg.
  • Print the values of count and avg using the print() function.

Python3




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.

RELATED ARTICLES

Most Popular

Recent Comments