Find Median of List in Python Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The median is the middle value in a sorted list. If the list has an odd number of items, it's the center one, if even, it's the average of the two middle values. It's used to find the center of data. Let's look at the different ways to find the median in Python.Using statistics.median() statistics.median() function returns the median of a list. It automatically handles both even and odd-length lists and gives the middle value after sorting the data. It's the easiest way to find the median. Python import statistics li = [4, 5, 8, 9, 10, 17] res = statistics.median(li) print(res) Output8.5 Explanation: statistics.median() calculates the middle value of the list automatically.Using sort()Sorts the list in ascending order, then finds the middle value based on list length to calculate the median. It's a simple and direct way to find the median. Python li = [4, 5, 8, 9, 10, 17] li.sort() n = len(li) if n % 2 == 0: res = (li[n//2 - 1] + li[n//2]) / 2 else: res = li[n//2] print(res) Output8.5 Explanation: The list li is sorted, then n = len(li) calculates its length. If n is even, (li[n//2 - 1] + li[n//2]) / 2 gives the median; else, li[n//2] is used.Using ~ operator Sort the list to find the middle value. The ~ operator helps access elements from the end, which makes it easier to get the correct index for the median. Python li = [4, 5, 8, 9, 10, 17] li.sort() mid = len(li) // 2 res = (li[mid] + li[~mid]) / 2 print(res) Output8.5 Explanation: List is sorted first.The middle index is found using mid = len(li) // 2 and ~mid gives the second middle index.Both values are averaged to calculate and print the median.Using python heapq.nlargest() or heapq.nsmallest()heapq.nlargest() and heapq.nsmallest() functions help find the largest and smallest elements in a list. It is used to get the middle values needed to calculate the median, especially in sorted or nearly sorted data. Python import heapq li = [4, 5, 8, 9, 10, 17] mid = len(li) // 2 if len(li) % 2 == 0: res = (heapq.nlargest(mid, li)[-1] + heapq.nsmallest(mid, li)[-1]) / 2 else: res = heapq.nlargest(mid+1, li)[-1] print(res) Output8.5 Explanation: For even length, gets two middle values using nlargest() and nsmallest(), then averages them.For odd length, picks the middle value using nlargest().Related Articles:Python Operatorsstatistics.median()heapq modulesort() Comment More infoAdvertise with us Next Article How to Find Index of Item in Python List M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python | Find number of lists in a tuple Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application. Method #1: Using len Python3 # Python code to find number of list in a tuple # Initial list Input1 = ([1, 2, 3, 4], [5, 6, 7, 8]) Input2 = ([1 4 min read How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it 2 min read Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a 2 min read Find the size of a list - Python In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get 2 min read Find the size of a list - Python In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get 2 min read Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e 3 min read Like