# 10 examples of 'python median' in Python

Every line of 'python median' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure. ## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``12def median(x):13    return int(np.median(x))``
``26def median(values):27    length = len(values)28    values.sort()29    if length % 2 != 0:30        # Odd number of values, so chose middle one31        return values[length/2]32    else:33        # Even number of values, so mean of middle two34        return mean([values[length/2], values[(length/2)-1]])``
``26def median(r):27    """Return the median of an iterable of numbers.2829    The median is the point at which half the numbers are lower than it and30    half the numbers are higher.  This gives a better sense of the majority31    level than the mean (average) does, because the mean can be skewed by a few32    extreme numbers at either end.  For instance, say you want to calculate33    the typical household income in a community and you've sampled four34    households:3536    &gt;&gt;&gt; incomes =        # Fast food crew37    &gt;&gt;&gt; incomes.append(24000)   # Janitor38    &gt;&gt;&gt; incomes.append(32000)   # Journeyman39    &gt;&gt;&gt; incomes.append(44000)   # Experienced journeyman40    &gt;&gt;&gt; incomes.append(67000)   # Manager41    &gt;&gt;&gt; incomes.append(9999999) # Bill Gates42    &gt;&gt;&gt; median(incomes)43    38000.044    &gt;&gt;&gt; mean(incomes)45    1697499.83333333334647    The median here is somewhat close to the majority of incomes, while the48    mean is far from anybody's income.49    50    This implementation makes a temporary list of all numbers in memory.51    """52    s = list(r)53    s_len = len(s)54    if s_len == 0:55        raise ValueError("can't calculate median of empty collection")56    s.sort()57    center = s_len // 258    is_odd = s_len % 259    if is_odd:60        return s[center]   # Return the center element.61    # Return the average of the two elements nearest the center.62    low = s[center-1]63    high = s[center]64    return mean([low, high])``
``106def median(x):107    return sorted(x)[len(x) // 2]``
``136def findMedian(self):137    small, large = self.heaps138    if len(large) &gt; len(small):139        return float(large)140    return (large - small) / 2.0``
``42def median(numbers):43   """Return the median of the list of numbers.4445   found at: http://mail.python.org/pipermail/python-list/2004-December/253517.html"""46   # Sort the list and take the middle element.47   n = len(numbers)48   copy = numbers[:] # So that "numbers" keeps its original order49   copy.sort()50   if n &amp; 1:         # There is an odd number of elements51      return copy[n // 2]52   else:53      return (copy[n // 2 - 1] + copy[n // 2]) / 2.0``
``318def median(self):319    return self.quantiles([0.5])[:,0]``
``995@property996def median(self):997    """Return the median."""998    return 0``
``91def median(lst):92    n = len(lst)93    if n &lt; 1:94            return None95    if n % 2 == 1:96            return sorted(lst)[n//2]97    else:98            return sum(sorted(lst)[n//2-1:n//2+1])/2.0``
``897def median(arr):898    """ Calculates the median value for each time series within tss.899900    :param arr: KHIVA array with the time series.901    :return: KHIVA array with the median value of each time series within tss.902    """903    b = ctypes.c_void_p(0)904    error_code = ctypes.c_int(0)905    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)906    KhivaLibrary().c_khiva_library.median(ctypes.pointer(arr.arr_reference),907                                          ctypes.pointer(b), ctypes.pointer(error_code), error_message)908    if error_code.value != 0:909        raise Exception(str(error_message.value.decode()))910911    return Array(array_reference=b)``