In this python programming tutorial, we will learn how to find out the mid number in 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 less than 4 and two elements 5,6 are greater than 4. Our list will contain an odd number of elements.
You can solve this problem by iterating through each element of the list one by one and testing 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 of 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 centre. Using the length of the list, we can find out the mid element easily.
Python comes with one inbuilt method ‘sort()’ to sort elements of a list in ascending or descending order. It doesn’t take any parameter but optionally you can pass one parameter ‘reverse’ to sort the list in reversed order.
If ‘reverse’ is ’True’, the list will be sorted in reversed order. Else, it will sort the list in increasing order, which is the default behavior.
sort() method doesn’t return any value. It will modify the original list.
In this example, we will sort the list in default order. Actually, we can sort it in any order we want. The mid element will be placed always in the middle if the list is sorted.
#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 from here.
The commented numbers in the above program denote the step numbers below :
- my_list is the given list, we need to find out the mid element of this list. Here, the list is already given. You can modify the program to populate the list by reading the list elements from the user. Take the length of the list from the user, use one for loop to read the items one by one and append them to the list.
- As explained above, using the .sort() method, we can sort a list in python. This method doesn’t return any value. It sorts all elements in the list we are calling sort() or it will modify the original list.
- Print out the sorted list.
- Print the middle value of the sorted list by accessing the size of the list/2 position. To get the length of a list, we are using len(list) method. len(list) method returns the length of a list. Dividing this value by 2 will give us the middle position.
- 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