Middle value of a list python

Why would you use a list comprehension? A list comprehension only knows about any one member of a list at a time, so that would be an odd approach. Instead:

def findMiddle(input_list):
    middle = float(len(input_list))/2
    if middle % 2 != 0:
        return input_list[int(middle - .5)]
    else:
        return (input_list[int(middle)], input_list[int(middle-1)])

This one should return the middle item in the list if it's an odd number list, or a tuple containing the middle two items if it's an even numbered list.

Edit:

Thinking some more about how one could do this with a list comprehension, just for fun. Came up with this:

[lis[i] for i in 
    range((len(lis)/2) - (1 if float(len(lis)) % 2 == 0 else 0), len(lis)/2+1)]

read as:

"Return an array containing the ith digit(s) of array lis, where i is/are the members of a range, which starts at the length of lis, divided by 2, from which we then subtract either 1 if the length of the list is even, or 0 if it is odd, and which ends at the length of lis, divided by 2, to which we add 1."

The start/end of range correspond to the index(es) we want to extract from lis, keeping in mind which arguments are inclusive/exclusive from the range() function in python.

If you know it's going to be an odd length list every time, you can tack on a [0] to the end there to get the actual single value (instead of an array containing a single value), but if it can or will be an even length list, and you want to return an array containing the two middle values OR an array of the single value, leave as is. :)

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

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 :

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

Middle value of a list python

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 :

Middle value of a list python

Middle value of a list python

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

How do you find the middle value of a list in Python?

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.

How do you find the mid of a list?

Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element.

How do I remove the middle element from a list in Python?

Remove an element from List by del. In python del is a keyword and can be used to delete the element in list by index i.e. It will delete the element at index 2 in list.

How do you slice a list in Python?

How to slice a list in Python.
Define the list you want to slice. Retrieve the list you're slicing into sublists or extended cells. ... .
Enter the "start" and "stop" indexes. ... .
Add a positive "step" value. ... .
Add a negative "step" value..