{"id":16043,"date":"2021-09-30T11:00:26","date_gmt":"2021-09-30T05:30:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=16043"},"modified":"2021-11-22T18:35:31","modified_gmt":"2021-11-22T13:05:31","slug":"python-program-to-find-median-of-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-median-of-list\/","title":{"rendered":"Python Program to Find Median of List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to get the Last Word from a String<\/a><\/p>\n Median:<\/strong><\/p>\n 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.<\/p>\n For example, let the given list is [ 1, 2, 4, 3, 6, 5].<\/p>\n The sorted form of the given list is [1, 2, 3, 4, 5, 6].<\/p>\n The median is the average of the middle two elements i.e.(3+4) \/2<\/strong>. Therefore, the Median is\u00a0 3.5.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Below are the ways to perform to find the median of a List in python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output<\/strong>:<\/p>\n The given list median value is printed.<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\nGiven List = [ 5, 2, 4 ,2, 1]<\/pre>\n
The Median of List of Elements = 2<\/pre>\n
Given List = [9, 6, 1, 4, 5, 2, 7, 8]<\/pre>\n
The Median of the Given List of Elements = 5.5<\/pre>\n
Program to Find Median of List :<\/h2>\n
\n
Method #1: Using Sort Function (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_list = [5, 2, 4, 2, 1]\r\n# sorting the given list using sort() function\r\ngvn_list.sort()\r\n# Calculate the length of the list using the len() function and store it in a variable.\r\nlst_leng = len(gvn_list)\r\n# Find the middle element of the given List by using (total length of list-1)\r\n# divided by 2( zero indexing)and store it in a variable.\r\nmid_vlu = (lst_leng-1)\/\/2\r\n# Check if the given list is even or odd using the If conditional statement.\r\nif(lst_leng % 2 == 0):\r\n # If the length of given list is even , median is the average of middle two elements.\r\n res_vlu = (gvn_list[mid_vlu] + gvn_list[mid_vlu+1])\/2\r\nelse:\r\n # If the length of given list is odd , Median\u00a0 is the middle element.\r\n res_vlu = gvn_list[mid_vlu]\r\nprint('The Median of the Given List of Elements =', res_vlu)\r\n<\/pre>\n
The Median of the Given List of Elements = 2<\/pre>\n
Method #2: Using Sort Function (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_list = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# sorting the given list using sort() function\r\ngvn_list.sort()\r\n# Calculate the length of the list using the len() function and store it in a variable.\r\nlst_leng = len(gvn_list)\r\n# Find the middle element of the given List by using (total length of list-1)\r\n# divided by 2( zero indexing)and store it in a variable.\r\nmid_vlu = (lst_leng-1)\/\/2\r\n# Check if the given list is even or odd using the If conditional statement.\r\nif(lst_leng % 2 == 0):\r\n # If the length of given list is even , median is the average of middle two elements.\r\n res_vlu = (gvn_list[mid_vlu] + gvn_list[mid_vlu+1])\/2\r\nelse:\r\n # If the length of given list is odd , Median\u00a0 is the middle element.\r\n res_vlu = gvn_list[mid_vlu]\r\nprint('The Median of the Given List of Elements =', res_vlu)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 9 6 4 1 5 2 7 8\r\nThe Median of the Given List of Elements = 5.5<\/pre>\n
\n