Open In App

Find sum and average of List in Python

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Similar Reads

Find average of a list in python
Given a list of numbers, the task is to find the average of that list. Average is the sum of elements divided by the number of elements. Input : [4, 5, 1, 2]Output : 3Explanation:Sum of the elements is 4+5+1+2 = 12 and total number of elements is 4.So average is 12/4 = 3Input : [15, 9, 55]Output : 26.33Explanation:Sum of the elements is 15+9+53 = 7
4 min read
Python Program to calculate sum and average of three numbers
We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20, 30Output: Sum=60, Average=20.0Input: 15, 29, 30Out
3 min read
Python | Average of each n-length consecutive segment in a list
Given a list, the task is to find the average of each n-length consecutive segment where each segment contains n elements. Example: Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Output: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] Explanation: Segment 1 - [1, 2, 3, 4, 5] => 15/5 = 3 Segment 2 - [2,
3 min read
Python | Record elements Average in List
Given a list of tuples, write a program to find average of similar tuples in list. Examples: Input: [('Geeks', 10), ('For', 10), ('Geeks', 2), ('For', 9), ('Geeks', 10)] Output: Resultant list of tuples: [('For', 9.5), ('Geeks', 7.333333333333333)] Input: [('Akshat', 10), ('Garg', 10), ('Akshat', 2), ('Garg', 9), ('Akshat', 10)] Output: Resultant l
3 min read
Python | Column Average in Record List
Sometimes the data that we receive, is in the form of tuples having data from various sources and we can usually have a use case in which we require to process the finding average of each index of tuple for cumulation. Let’s discuss certain ways in which this can be performed. Method #1: Using list comprehension This is the most naive method to per
4 min read
Python - Rear elements Average in List
Sometimes, while working with data, we can have a problem in which we need to perform the mean of all the rear elements that come after K. This can be an application in Mathematics and Data Science domain. Let us discuss certain ways in which this task can be performed. Method #1 : Using sum() + list comprehension The combination of the above funct
6 min read
Python - Average digits count in a List
Given a list of elements extract the average digit count in List. Input : test_list = [34, 2345, 23, 456, 2, 23, 456787] Output : 2.857142857142857 Explanation : Average of all digit count. [2+4+2+3+1+2+6 = 20, 20/7 = 2.857142857142857] Input : test_list = [34, 1, 456]Output : 2.0 Explanation : Average of all digit count. [1 + 2 + 3 = 6, 6 / 3 = 2]
4 min read
Python | Average String length in list
Sometimes, while working with data, we can have a problem in which we need to gather information of average length of String data in list. This kind of information might be useful in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + sum() + len() The combination of above func
8 min read
Find the average of an unknown number of inputs in Python
Prerequisites: *args and **kwargs in Python The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyword, variable-length argument list. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the wo
3 min read
Find Average of a Number Digits in Python
AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is an integer: Follow the below steps to solve the pr
3 min read
three90RightbarBannerImg