Python – Rear elements Average in List
Last Updated :
28 Apr, 2023
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 functionalities can be used to perform this task. In this, we first let the initial K element be as they are and perform the sum of the rest of element and divide by the number of elements left.
Python3
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
res = test_list[: K] + [ sum (test_list[K:]) / len (test_list[K:])]
print ( "Average List after K elements : " + str (res))
|
Output :
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using mean() + list comprehension
This is yet another way in which this task can be performed. In this, we perform the task of finding mean using mean(), rest of task is the same as the above method.
Python3
from statistics import mean
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
res = [ * test_list[:K], mean(test_list[K:])]
print ( "Average List after K elements : " + str (res))
|
Output :
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method#3: Using NumPy module
Python3
import numpy as np
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
res = test_list[: K] + [np.mean(test_list[K:])]
print ( "Average List after K elements : " + str (res))
|
Output :
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 Using reduce function:
Python3
from functools import reduce
def avg_after_k(lst, k):
avg = reduce ( lambda x, y: x + y, lst[k:]) / len (lst[k:])
return lst[:k] + [avg]
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
k = 3
result = avg_after_k(test_list, k)
print (result)
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using for loop
Approach:
- Initialize the input list, test_list.
- Print the original list.
- Initialize K to the value 3.
- Initialize sum_of_rear_elements to 0 and count_of_rear_elements to 0.
- Use a for loop to iterate over the elements in the list starting from index K and add each element to sum_of_rear_elements.
- Increment the count_of_rear_elements by 1 for each element in the loop.
- Compute the average of the rear elements by dividing sum_of_rear_elements by count_of_rear_elements.
- Create a new list, res, that contains the first K elements of test_list and the average of the rear elements.
- Print the result list, res.
Below is the implementation of the above approach:
Python3
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
sum_of_rear_elements = 0
count_of_rear_elements = 0
for i in range (K, len (test_list)):
sum_of_rear_elements + = test_list[i]
count_of_rear_elements + = 1
res = test_list[:K] + [sum_of_rear_elements / count_of_rear_elements]
print ( "Average List after K elements : " + str (res))
|
Output
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time complexity: O(n), where n is the length of the list
Auxiliary space: O(1), as we are using constant extra space.
Method #6: Using slicing and sum() function.
Approach:
- First initializing the input list test_list and printing it. We are also initializing the value of K.
- Calculating the sum of the elements from the Kth index till the end of the list using slicing and the sum() function. We are also counting the number of rear elements by getting the length of the sliced list.
- Finally, calculate the average of the rear elements and appending it to a new list res along with the first K elements of the original list using the slicing operator.
- The result is then printed.
Below is the implementation of the above approach:
Python3
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
sum_of_rear_elements = sum (test_list[K:])
count_of_rear_elements = len (test_list[K:])
res = test_list[:K] + [sum_of_rear_elements / count_of_rear_elements]
print ( "Average List after K elements : " + str (res))
|
Output
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time Complexity: O(N), where N is the length of the input list test_list.
Auxiliary Space: O(1), as we are not using any additional data structures to solve the problem.
Method #7: Using itertools.islice() function
This method involves using the itertools.islice() function to slice the list and calculate the average of the remaining elements.
Python3
import itertools
test_list = [ 5 , 6 , 4 , 7 , 8 , 1 , 10 ]
print ( "The original list is : " + str (test_list))
K = 3
sum_of_rear_elements = sum (itertools.islice(test_list, K, None ))
count_of_rear_elements = len (test_list[K:])
res = test_list[:K] + [sum_of_rear_elements / count_of_rear_elements]
print ( "Average List after K elements : " + str (res))
|
Output
The original list is : [5, 6, 4, 7, 8, 1, 10]
Average List after K elements : [5, 6, 4, 6.5]
Time complexity: O(n)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...