Program to find average salary excluding the minimum and maximum salary in Python

Suppose we have an array with distinct elements called salary where salary[i] is the salary of ith employee. We have to find the average salary of employees excluding the minimum and maximum salary.

So, if the input is like salary = [8000, 6000, 2000, 8500, 2500, 4000], then the output will be 5125.0, as the minimum and maximum salary values are 2000 and 8500. Excluding them, the remaining salary values are [8000, 6000, 2500, 4000], so the average is (8000 + 6000 + 2500 + 4000)/4 = 5125.

Algorithm

To solve this problem, we will follow these steps −

  • Remove minimum salary from the list

  • Remove maximum salary from the list

  • Return sum of remaining salary values divided by the count of remaining elements

Method 1: Using remove() Method

This approach directly modifies the original list by removing minimum and maximum values ?

def solve(salary):
    salary.remove(min(salary))
    salary.remove(max(salary))
    return sum(salary) / len(salary)

salary = [8000, 6000, 2000, 8500, 2500, 4000]
result = solve(salary.copy())  # Use copy to preserve original
print(f"Average salary excluding min/max: {result}")
Average salary excluding min/max: 5125.0

Method 2: Without Modifying Original List

This approach calculates the average without changing the original list ?

def calculate_average(salary):
    min_salary = min(salary)
    max_salary = max(salary)
    total = sum(salary) - min_salary - max_salary
    count = len(salary) - 2
    return total / count

salary = [8000, 6000, 2000, 8500, 2500, 4000]
result = calculate_average(salary)
print(f"Original list: {salary}")
print(f"Average salary excluding min/max: {result}")
Original list: [8000, 6000, 2000, 8500, 2500, 4000]
Average salary excluding min/max: 5125.0

Method 3: Using List Comprehension

This approach filters out the minimum and maximum values using list comprehension ?

def find_average(salary):
    min_val, max_val = min(salary), max(salary)
    filtered_salary = [s for s in salary if s != min_val and s != max_val]
    return sum(filtered_salary) / len(filtered_salary)

salary = [8000, 6000, 2000, 8500, 2500, 4000]
result = find_average(salary)
print(f"Filtered salaries: {[s for s in salary if s != min(salary) and s != max(salary)]}")
print(f"Average: {result}")
Filtered salaries: [8000, 6000, 2500, 4000]
Average: 5125.0

Comparison

Method Modifies Original Time Complexity Best For
remove() method Yes O(n) Simple, direct approach
Mathematical calculation No O(n) Preserving original data
List comprehension No O(n) Functional programming style

Conclusion

All three methods effectively calculate the average salary excluding minimum and maximum values. Use the mathematical approach when you need to preserve the original list, or use remove() for simpler code when modification is acceptable.

Updated on: 2026-03-25T20:16:54+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements