Python - Rear elements Average in List
Last Updated :
28 Apr, 2025
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
# Python3 code to demonstrate
# Rear elements Average in List
# using list comprehension + sum()
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using list comprehension + sum()
res = test_list[: K] + [sum(test_list[K:]) / len(test_list[K:])]
# Printing result
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
# Python3 code to demonstrate
# Rear elements Average in List
# using list comprehension + mean()
from statistics import mean
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using list comprehension + mean()
res = [*test_list[:K], mean(test_list[K:])]
# printing result
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
# Python3 code to demonstrate
# Rear elements Average in List
# using numpy
import numpy as np
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using numpy mean function
res = test_list[: K] + [np.mean(test_list[K:])]
# printing result
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
# Import the reduce function from the functools module
from functools import reduce
def avg_after_k(lst, k):
# Compute the average of elements in lst[k:]
avg = reduce(lambda x, y: x + y, lst[k:]) / len(lst[k:])
# Return the first k elements of lst concatenated with the average
return lst[:k] + [avg]
# Example usage
test_list = [5, 6, 4, 7, 8, 1, 10]
k = 3
# Call the avg_after_k function
result = avg_after_k(test_list, k)
# Print the result
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
# Python3 code to demonstrate
# Rear elements Average in List
# using for loop
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using for loop
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]
# Printing result
print("Average List after K elements : " + str(res))
OutputThe 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
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using slicing and sum() function
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]
# printing result
print("Average List after K elements : " + str(res))
OutputThe 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
# Initializing list
test_list = [5, 6, 4, 7, 8, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Rear elements Average in List
# using itertools.islice() function
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]
# printing result
print("Average List after K elements : " + str(res))
OutputThe 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)
Similar Reads
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)
3 min read
Column Average in Record List - Python Given a list of records where each record contains multiple fields, the task is to compute the average of a specific column. Each record is represented as a dictionary or a list, and the goal is to extract values from the chosen column and calculate their average. Letâs explore different methods to
3 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 comprehen
8 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 : Aver
4 min read
Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using
2 min read
Python - Index Ranks of Elements Given a list of elements, our task is to get the index ranks of each element. Index Rank of Number = (Sum of occurrence indices of number) / number Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9] Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333
3 min read