Open In App

Python – Rear elements Average in List

Last Updated : 28 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

[5, 6, 4, 6.5]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using for loop

Approach:

  1. Initialize the input list, test_list.
  2. Print the original list.
  3. Initialize K to the value 3.
  4. Initialize sum_of_rear_elements to 0 and count_of_rear_elements to 0.
  5. 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.
  6. Increment the count_of_rear_elements by 1 for each element in the loop.
  7. Compute the average of the rear elements by dividing sum_of_rear_elements by count_of_rear_elements.
  8. Create a new list, res, that contains the first K elements of test_list and the average of the rear elements.
  9. 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))


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:

  1. First initializing the input list test_list and printing it. We are also initializing the value of K.
  2. 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.
  3. 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. 
  4. 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))


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


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
Previous
Next
Share your thoughts in the comments

Similar Reads