How do you find the middle value of 3 numbers in python?

Given 3 numbers, I need to find which number lies between the two others.

ie,given 3,5,2 I need 3 to be returned.

I tried to implement this by going thru all three and using if else conditions to check if each is between the other two.But this seems a naive way to do this.Is there a better way?

asked Apr 2, 2012 at 15:55

2

Put them in a list, sort them, pick the middle one.

answered Apr 2, 2012 at 15:56

Sven MarnachSven Marnach

543k114 gold badges914 silver badges816 bronze badges

5

>>> x = [1,3,2]
>>> sorted(x)[len(x) // 2]
2

answered Apr 2, 2012 at 16:01

How do you find the middle value of 3 numbers in python?

jamylakjamylak

123k29 gold badges227 silver badges227 bronze badges

The fastest obvious way for three numbers

def mean3(a, b, c):
    if a <= b <= c or c <= b <= a:
        return b
    elif b <= a <= c or c <= a <= b:
        return a
    else:
        return c

answered Apr 2, 2012 at 16:45

Zaur NasibovZaur Nasibov

21.8k11 gold badges51 silver badges80 bronze badges

1

What you want is the median. You can use this code below for any number of numbers:

import numpy
numbers = [3,5,2]
median = numpy.median(numbers)

for a custom solution you can visit this page.

answered Apr 2, 2012 at 16:08

Thanasis PetsasThanasis Petsas

4,2785 gold badges30 silver badges56 bronze badges

2

You could do

numbers = [3, 5, 2]
sorted(numbers)[1]

answered Apr 2, 2012 at 15:57

How do you find the middle value of 3 numbers in python?

David RobinsonDavid Robinson

75.5k15 gold badges162 silver badges180 bronze badges

1

This is a O(n) implementation of the median using cumulative distributions. It's faster than sorting, because sorting is O(ln(n)*n).

def median(data):
    frequency_distribution = {i:0 for i in data}
    for x in data:
        frequency_distribution[x] =+ 1
    cumulative_sum = 0
    for i in data:
        cumulative_sum += frequency_distribution[i]
        if (cumulative_sum > int(len(data)*0.5)):
            return i

answered Apr 2, 2012 at 16:15

luke14freeluke14free

2,5011 gold badge16 silver badges25 bronze badges

2

Check this (Suppose list already sorted):

def median(list):
    ceil_half_len = math.ceil((len(list)-1)/2)   # get the ceil middle element's index
    floor_half_len = math.floor((len(list)-1)/2) # get the floor middle element 's index'
    return (list[ceil_half_len] + list[floor_half_len]) / 2

answered Oct 29, 2013 at 3:29

1

If it's just for 3 numbers. First find the max and the min numbers in list. Remove them, the remainder of list is the medium. If you don't want to use the "if-else" or "sorted()" that is.

def mid(a,b,c):
    num = [a,b,c]
    small, big = min(num), max(num)
    num.remove(small)
    num.remove(big)
    return num[0] # return the remainder

answered Apr 25, 2020 at 7:38

How do you find the middle value of 3 numbers in python?

samvsamv

112 silver badges3 bronze badges

Here is my attempt using a more pythonic version

def median(a):
    sorted_a = sorted(a)
    if len(a) % 2 == 0:
        median = sum(sorted_a[(len(a)//2)-1:(len(a)//2)+1])/2.
    else:
        median = sorted_a[(len(a)-1)//2]

>>> x = [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060]
>>> median(x)
>>> 44627.5
>>> y = [1, 2, 3]
>>> median(y)
>>> 2

answered Jan 12, 2017 at 7:28

MichaelMichael

2,2721 gold badge32 silver badges53 bronze badges

If you wish to avoid sorting, you can do:

def find_median(x):
    return sum(x) - max(x) - min(x)

answered Mar 30, 2017 at 19:11

Cabbage soupCabbage soup

1,2321 gold badge18 silver badges26 bronze badges

How do you find the middle number in Python?

“how to find middle value of a python list” Code Answer's.
def findMiddle(input_list):.
middle = float(len(input_list))/2..
if middle % 2 != 0:.
return input_list[int(middle - .5)].
return (input_list[int(middle)], input_list[int(middle-1)]).

How do you find the median of 3 numbers in Python?

Write a Python program to find the median of three values.
if num1 < num3: median = num1..
elif b > num3: median = num2..
else: median = num3..
if num1 > num3: median = num1..
elif num2 < num3: median = num2..
else: median = num3..

How do you find the median of three numbers?

To find the median of any set of numbers, put them in order from smallest to greatest. If a number occurs more than once, list it more than once. The number in the middle is the median. If there is an even number of numbers, the median is the average of the two numbers in the middle.