Python program to find the middle element of a random number list :

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.

 

Algorithm :

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 element 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.

 

Sorting a list in python :

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.

 

Python program :

You can download this program from here.

 

python find middle element random list

 

Explanation :

The commented numbers in the above program denote the step numbers below :

1. 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.

2. 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.

3. Print out the sorted list.

4. 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.

Output :

python find middle element random list

python find mid number

 

You might also like :

Categories: python

2 Comments

subbu · October 15, 2018 at 5:43 am

list=[1,2,3,4,5,6,7,8,9,10]
i want to find middle number in a list.Here 10 numbers are there i want output [5,6] like this.Can u please explain it

admin · October 17, 2018 at 3:24 am

you can first check the length of the list. If is odd then it will be same as like we have shown in this example. If it is even then you can print the numbers : my_list[len(my_list)/2)] and my_list[len(my_list)/2) – 1]

Leave a Reply

Your email address will not be published. Required fields are marked *