Q: Most efficient way to get average of all elements in list where each element occurs at least half as many times as the mode of the list I have a specific task to perform in python. Efficiency and speed are the most important here which is why I'm posting the question. 
I need to get the average of items in a list, but only of the items that occur as least half as many times as the mode of the list occurs. 
For instance if the list is [1,2,2,3,4,4,4,4] I need to get the average of 2,2,4,4,4,4. Since 4 is the mode of the list and occurs four times, the only element that occurs at least half of four times (twice) is 2. Thus I discount all occurrences 1 and 3 and average the list. 
I'm not sure what the most efficient way to do this is. I know how to brute force calculate the solution, but that's obviously not the quickest implementation. 
I thought it may be best to use a numpy array, but since I'll be appending to the list quite frequently, I didn't think this would be the best choice. 
My other thoughts have been to possibly use a Counter based approach from the collections module. But once again, I don't know if it's the fastest or most sensible for performing a rather weird calculation like this. 

A: To get the mode of the list, you must iterate through the whole list at least once (Technically, you could stop as soon as the count of one of the elements is more than the remaining items in the list, but the efficiency is negligible).
Python has an efficient and easy way to do this with Counter.
from __future__ import division
from collections import Counter
from itertools import islice

data = [1,2,2,3,4,4,4,4]
c = Counter(data)

# Get the mode
mode, mode_n = c.most_common(1)[0]

# Store the cumulative sum and count so we can compute the mean
# Process the most common element (the mode) first since we
# already have that data.
cumulative_sum = mode * mode_n
cumulative_n = mode_n

# Process the remaining elements. most_common returns the remaining
# elements and their counts in descending order by the number of times
# the appear in the original list.  We can skip the first element since
# we've already processed it.  As soon as an element is less numerous
# than half the mode, we can stop processing further elements.
for val, val_n in islice(c.most_common(), 1, None):
    if val_n < mode_n / 2:
        break
    cumulative_sum += val * val_n
    cumulative_n += val_n

# Compute the Mean
avg = cumulative_sum / cumulative_n

The only thing I'm not completely sure of is how you treat modes that appear an odd number of times.  If the mode appeared 5 times, do you round up to 3 or down to 2 when checking the other elements?
Currently, it's rounding up, but if you wanted to round down, you could just change it to this:
if val_n < mode_n // 2:


A: If you decide to use numpy, here's a concise method using numpy.unique and numpy.average:
In [54]: x = np.array([1, 2, 2, 3, 4, 4, 4, 4])

In [55]: uniqx, counts = np.unique(x, return_counts=True)

In [56]: keep = counts >= 0.5*counts.max()

In [57]: np.average(uniqx[keep], weights=counts[keep])
Out[57]: 3.3333333333333335

Note that np.unique sorts its argument, so its time complexity is O(n*log(n)), while the problem can be solved with an algorithm that is O(n).  Do some timing comparisons using arrays with lengths that will be typical before you rule out this method based on its asymptotic time complexity.
