Program to Find Median of List

Python Program to Find Median of List

In the previous article, we have discussed Python Program to get the Last Word from a String

Median:

In general, the median is the middle value of a sorted list of elements. To find the median, we must first sort the data if it is not already sorted. The middle value can then be extracted and printed. If the number of elements in the list is even, we can calculate the median by taking the average of the list’s two middle values. Else take the middle value.

For example, let the given list is [ 1, 2, 4, 3, 6, 5].

The sorted form of the given list is [1, 2, 3, 4, 5, 6].

The median is the average of the middle two elements i.e.(3+4) /2. Therefore, the Median is  3.5.

Examples:

Example1:

Input: 

Given List = [ 5, 2, 4 ,2, 1]

Output:

The Median of List of Elements = 2

Example2:

Input:

Given List = [9, 6, 1, 4, 5, 2, 7, 8]

Output:

The Median of the Given List of Elements = 5.5

Program to Find Median of List :

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Below are the ways to perform to find the median of a List in python.

Method #1: Using Sort Function (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Sort the given List using the sort( ) function.
  • Calculate the length of the list using the len() function and store it in a variable.
  • Find the given list’s middle element by using (total length of list-1) divided by 2( zero indexing) and store it in a variable.
  • Check if the given list is even or odd using the If conditional statement.
  • If the given list’s length is even, the median is the average of the middle two elements.
  • If the length of the given list is odd, Median is the middle element.
  • Print the median of the above-given List.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_list = [5, 2, 4, 2, 1]
# sorting the given list using sort() function
gvn_list.sort()
# Calculate the length of the list using the len() function and store it in a variable.
lst_leng = len(gvn_list)
# Find the middle element of the given List by using (total length of list-1)
# divided by 2( zero indexing)and store it in a variable.
mid_vlu = (lst_leng-1)//2
# Check if the given list is even or odd using the If conditional statement.
if(lst_leng % 2 == 0):
    # If the length of given list is even , median is the average of middle two elements.
    res_vlu = (gvn_list[mid_vlu] + gvn_list[mid_vlu+1])/2
else:
    # If the length of given list is odd , Median  is the middle element.
    res_vlu = gvn_list[mid_vlu]
print('The Median of the Given List of Elements =', res_vlu)

Output:

The Median of the Given List of Elements = 2

Method #2: Using Sort Function (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Sort the given List using the sort( ) function.
  • Calculate the length of the list using the len() function and store it in a variable.
  • Find the given list’s middle element by using (total length of list-1) divided by 2( zero indexing) and store it in a variable.
  • Check if the given list is even or odd using the If conditional statement.
  • If the given list’s length is even, the median is the average of the middle two elements.
  • If the length of the given list is odd, Median is the middle element.
  • Print the median of the above-given List.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_list = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# sorting the given list using sort() function
gvn_list.sort()
# Calculate the length of the list using the len() function and store it in a variable.
lst_leng = len(gvn_list)
# Find the middle element of the given List by using (total length of list-1)
# divided by 2( zero indexing)and store it in a variable.
mid_vlu = (lst_leng-1)//2
# Check if the given list is even or odd using the If conditional statement.
if(lst_leng % 2 == 0):
    # If the length of given list is even , median is the average of middle two elements.
    res_vlu = (gvn_list[mid_vlu] + gvn_list[mid_vlu+1])/2
else:
    # If the length of given list is odd , Median  is the middle element.
    res_vlu = gvn_list[mid_vlu]
print('The Median of the Given List of Elements =', res_vlu)

Output:

Enter some random List Elements separated by spaces = 9 6 4 1 5 2 7 8
The Median of the Given List of Elements = 5.5

The given list median value is printed.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.