Python program to find the middle element of an unsorted random number list:
In this Python programming tutorial, we will learn how to find out the middle number of a number list. For example, if the list is [1,6,5,4,3], the mid element is 4. Because two numbers 1,3 are smaller than 4 and two numbers 5,6 are bigger than 4. The list will always contain an odd number of elements.
Algorithm:
You can solve this problem by iterating through each element of the list one by one and by verifying each element if it is the middle element or not. We can check it by comparing the count of all smaller and bigger elements in the list. If for a number, the list contains an equal amount of smaller and bigger numbers, it will be the middle number in that list. The main problem with this solution is that we need to iterate through the list multiple times.
Another way and the most preferred way to solve this problem is by sorting the list. If we sort the list, it will move the middle element to the center of the list. By using the length of the list, we can find out the mid element easily.
How to sort a list in Python:
Python comes with one inbuilt method sort()
to sort the elements of a list in ascending or descending order. It doesn’t take any parameters and sorts the list in ascending order by default. Optionally you can pass one parameter reverse
to sort the list in reversed order.
If the reverse
parameter is True
, the list will be sorted in reversed order. Else, it will sort the list in increasing order, which is the default behavior.
The sort()
method doesn’t return any value. It will modify the original list.
In this example, we will sort the list in default order. We can sort it in any order we want. The middle element will be placed always in the middle if the list is sorted.
Python program :
The following element finds the middle element of a list with random unsorted values:
#1
my_list = [4,3,2,9,10,44,1]
#2
my_list.sort()
#3
print("sorted list is ",my_list)
#4
print("mid value is ",my_list[int(len(my_list)/2)])
You can also download this program on Github
Explanation :
The commented numbers in the above program denote the step numbers below:
-
The original list is assigned to the
my_list
variable. We need to find out the middle element of this list. You can change the program to populate the list by reading the list elements from the user. We can take the length of the list from the user and with a loop, we can read the items from the user and append them to the list. -
As explained above, by using the
sort()
method, we are sorting the listmy_list
. This method doesn’t return any value. It sorts all elements in the list by modifying the original list. -
Print out the sorted list.
-
Print the middle element of the sorted list by accessing the position of the mid element in the list. To get the length of the
list
, we are using thelen(list)
method. We are dividing the length of this list by 2 to get the middle element’s index.
Output :
Similar tutorials :
- Find total number of lowercase characters in a string using Python 3
- Python program to find the circumference of a circle
- Python program to find the sum of all values of a dictionary
- Python program to find the maximum and minimum element in a list
- Python program to find the multiplication of all elements in a list
- Python program to find the square root of a number