Finding the "centered average" of a list


“Return the “centered” average of a list of integers, which we’ll say is the mean average of the values, except ignoring the largest and smallest values in the list. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use integer division to produce the final average. You may assume that the list is length 3 or more.”

This is a problem I have from my homework assignment and I am stumped at how to find the the largest/smallest numbers and cut them out of the list. Here is what I have so far. and It works for 10/14 the scenarios that I have to pass.. I think it is just because it grabs the median

def centered_average(nums):
x = 0
for i in range(len(nums)):
    x = i + 0
y = x + 1
if y%2 == 0:
    return (nums[y/2] + nums[(y/2)+1]) / 2
    return nums[y/2]
Asked By: SleBluue



If the list isn’t too long, it shouldn’t be too computationally expensive to sort the list:


Then you can create a new list without the first and last entries, which will be the smallest and largest values:

new_nums = sorted(nums)[1:-1] # from index 1 to the next-to-last entry
Answered By: Simon Fraser

If I understand the question, this should work:

def centered_average(nums):
  trim = sorted(nums)[1:-1]
  return sum(trim) / len(trim)
Answered By: crow_t_robot

Sorting the array is certainly terser code, here’s an alternative with a manual loop

    max_value = nums[0]
    min_value = nums[0]
    sum = 0
    for x in nums:
        max_value = max(max_value, x)
        min_value = min(min_value, x)
        sum += x

    return (sum - max_value - min_value) / (len(nums) - 2)

This just adds everything in and removes the max and min at the end.

Answered By: SirGuy

Before i start i know there are easier ways mentioned in the other answers using the function sort, yes that is true but i believe your teacher must have iven you this to able to master loops and use them logically.

First pick your first number and assign it to high and low, don’t worry it will make sense afterwards.

def centered average(nums):

high = nums[0]
small = nums[0]

Here is were the magic happens, you loop through your list and if the number your on in the loop is larger then the previous ones then you can replace the variable high with it, let me demonstrate.

for count in nums:
    if count > high:
        high = count
    if count < low:
        low = count

Now you have the low and the high all you do is add the values of the loop together minus the high and the low (as you said you do not need them).Then divide that answer by len of nums.

for count in nums:
    sum = count + sum
sum = sum - (high + low)
return sum
Answered By: Ziad Fakhoury
def centered_average(nums):
  nums = sorted(nums)
  for i in range(len(nums)):
    if len(nums)%2 != 0:
      return nums[len(nums)/2]
      return ((nums[len(nums)/2] + nums[len(nums)/2 - 1]) / 2)
Answered By: to_chins

This is a very sub standard solution to the problem. This code is a bad code that does not take into account any consideration for complexity and space. But I think the thought process to be followed is similar to the steps in the code. This then can be refined.

def centered_average(nums):
#Find max and min value from the original list
max_value = max(nums)
min_value = min(nums)
#counters for counting the number of duplicates of max and min values.
mx = 0
mn = 0
sum = 0
#New list to hold items on which we can calculate the avg
new_nums = []
#Find duplicates of max and min values
for num in nums:
  if num == max_value:
    mx += 1
  if num == min_value:
    mn += 1
#Append max and min values only once in the new list
if mx > 1:
if mn > 1:
#Append all other numbers in the original to new list
for num in nums:
  if num != max_value and num != min_value:
#Calculate the sum of all items in the list
for new in new_nums:
  sum += new
#Calculate the average value.
avg = sum/len(new_nums)

return avg
Answered By: Arpit Patil
def centered_average(nums):
    maximums = []
    minimums = []
    sum_of_numbers = 0
    length =len(nums) + (len(minimums)-1) + (len(maximums)-1)
    for i in nums:
        if i == max(nums):
        elif i == min(nums):
            sum_of_numbers += i
    if len(maximums)>=2 or len(minimums)>=2:
        sum_of_numbers = sum_of_numbers + (max(nums)*(len(maximums)-1))(min(nums)*(len(minimums)-1))
    return sum_of_numbers / length
Answered By: Aleksi Immonen
def centered_average(nums):
  for item in nums:
    if item > max1:
        max1 = item
    if item < min1:
        min1 = item
  return sum1
Answered By: Inka

simple solution

def centered_average(nums):



  return av
Answered By: Thaneesh reddy
  • use sum function to sum the array
  • max and min functions to get the biggest and smallest number

    def centered_average(nums):
        return (sum(nums) - max(nums) - min(nums)) / (len(nums) - 2)
Answered By: Somebody
def centered_average(nums):
    sorted_list = sorted(nums)
    return sum(sorted_list[1:-1])/(len(nums)-2)

This will get the job done.

Answered By: Srinath

Python 3 Solution using list.index, list.pop, min and max functions.

def solution(input):

    average = 0

    minimum = min(input)
    maximum = max(input)


    average =  round(sum(input) / len(input))

    return average
Answered By: Rithvik M

New here. I like to check my solutions with solutions found on the internet and did not see my code here yet (hence the post). I found this challenge on And here is my solution:
** This is done in Python 3.9.1.

First the min and max are popped from the list with the index method. After it’s just a simple avg calculation.

def centered_average(nums):

  return sum(nums)/len(nums)
Answered By: Martijn
def centered_average(nums):
    count = 0
    for i in range(len(new_nums)):
    return ans
Answered By: Anthony Onuorah
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.