How do you find the median of a list in Python? The list can be of any size and the numbers are not guaranteed to be in any particular order.
If the list contains an even number of elements, the function should return the average of the middle two.
Here are some examples (sorted for display purposes):
median([1]) == 1 median([1, 1]) == 1 median([1, 1, 2, 4]) == 1.5 median([0, 2, 5, 6, 8, 9, 9]) == 6 median([0, 0, 0, 0, 4, 4, 6, 8]) == 2
Related Posts:
- How to sort a list of objects based on an attribute of the objects?
- How to sort a list/tuple of lists/tuples by the element at a given index?
- Python data structure sort list alphabetically
- Sorting list based on values from another list
- How can I reverse a list in Python?
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- how to sort pandas dataframe from one column
- Understanding slice notation
- TypeError: list indices must be integers or slices, not str
- How do I sort a dictionary by value?
- How do I sort a dictionary by value?
- IndexError: list index out of range and python
- How to convert list to string [duplicate]
- Finding the index of an item in a list
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- What does the list() function do in Python?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Check if something is (not) in a list in Python
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How do I concatenate two lists in Python?
- How can I randomly select an item from a list?
- Print a list in reverse order with range()?
- Fastest way to check if a value exists in a list
- How can I count the occurrences of a list item?
- TypeError: ‘list’ object cannot be interpreted as an integer
- Why is it string.join(list) instead of list.join(string)?
- Get list from pandas dataframe column or row?
- Finding and replacing elements in a list
- ValueError: max() arg is an empty sequence
- Remove list from list in Python
- How can I compare two lists in python and return matches
- Syntax behind sorted(key=lambda: …)
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Python: Array v. List
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- Writing a list to a file with Python
- How do I check if a list is empty?
- Using a comparator function to sort
- Understanding slice notation
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- Converting Dictionary to List?
- Python Sets vs Lists
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to deep copy a list?
- Python return list from function
- How to check if a string is a substring of items in a list of strings?
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How do I check if a list is empty?
- How can I get list of values from dict?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- Pop index out of range
- Print list without brackets in a single row
- How to iterate through two lists in parallel?
- How to remove \n from a list element?
- How to read a text file into a list or an array with Python
- Map to List error: Series object not callable
- how to create a list of lists
- Python assigning multiple variables to same value? list behavior
- How do I check if there are duplicates in a flat list?
- How to return dictionary keys as a list in Python?
- Append a tuple to a list – what’s the difference between two ways?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- How can I remove all instances of an element from a list in Python?
- Create an empty list in Python with certain size
- How to concatenate items in a list to a single string?
- What’s the difference between lists enclosed by square brackets and parentheses in Python?
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- How to make a set of lists
- Appending to a list gives ‘int’ object has no attribute ‘append’
- TypeError: unsupported format string passed to list.__format__
- numpy-equivalent of list.pop?
- Efficiently sorting a numpy array in descending order?
- How to create a numpy array of lists?
- convert csv file to list of dictionaries
- Element-wise addition of 2 lists?
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- Sorting arrays in NumPy by column
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- Get difference between two lists
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- Building a list inside a list in python
- Writing Python lists to columns in csv
- How do operator.itemgetter() and sort() work?