Python – Mean deviation of Elements
Last Updated :
28 Feb, 2023
Given a list, the task is to write a Python program to compute how deviated are each of them from its list mean.
Examples:
Input : test_list = [7, 5, 1, 2, 10, 3]
Output : [2.333333333333333, 0.33333333333333304, 3.666666666666667, 2.666666666666667, 5.333333333333333, 1.666666666666667]
Explanation : Mean is 4.66667, related differences are computed.
Input : test_list = [1, 2, 3, 4, 5]
Output : [2, 1, 0, 1, 2]
Explanation : Mean is 3, related differences are computed.
Method #1 : Using loop + mean() + abs()
In this, we perform iteration of each element and compute deviation from mean using abs(), the computation of mean is done using mean().
Python3
from statistics import mean
test_list = [ 7 , 5 , 1 , 2 , 10 , 3 ]
print ( "The original list is : " + str (test_list))
res = []
mean_val = mean(test_list)
for ele in test_list:
res.append( abs (ele - mean_val))
print ( "Mean deviations : " + str (res))
|
Output:
The original list is : [7, 5, 1, 2, 10, 3] Mean deviations : [2.333333333333333, 0.33333333333333304, 3.666666666666667, 2.666666666666667, 5.333333333333333, 1.666666666666667]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + mean()
In this similar functionalities are used as above function, difference being list comprehension is used as one-liner to solve this problem.
Python3
from statistics import mean
test_list = [ 7 , 5 , 1 , 2 , 10 , 3 ]
print ( "The original list is : " + str (test_list))
res = []
mean_val = mean(test_list)
res = [ abs (ele - mean_val) for ele in test_list]
print ( "Mean deviations : " + str (res))
|
Output:
The original list is : [7, 5, 1, 2, 10, 3] Mean deviations : [2.333333333333333, 0.33333333333333304, 3.666666666666667, 2.666666666666667, 5.333333333333333, 1.666666666666667]
Time Complexity: O(n)
Space Complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...