29

How would I find the exact middle of a python list?

aList = [1,2,3,4,5]
middle = findMiddle(aList)
print middle

This is just an example of a function that would be able to find the middle of any list, is this something you can do using list comprehension?

Edit: This is different than taking out the middle point because I would like to just print out the middle value, if the list is odd I would like to return both the middle values like the accepted answer. Not getting the median like the other question has asked and getting the average of the two values.

Ali
  • 633
  • 1
  • 9
  • 20
  • 1
    you can use list comprehension but why not just use plain old math? – taesu Jun 30 '16 at 18:40
  • 2
    What's the middle of `[1,2,3,4]`? – chepner Jun 30 '16 at 18:40
  • 3
    how about a[len(a)/2 ] ? – dnit13 Jun 30 '16 at 18:41
  • I'm not clear on what you want. Do you want a single item that appears at the midpoint of the list? Do you want the index of that item? Do you want the interior one-third of the items in the list? Do you want something else? What is the desired output for `findMiddle([70, 51, 59, 19, 84, 91, 69, 84, 26])`? Is it `4`? Is it `84`? Is it `[19, 84, 91]`? – Kevin Jun 30 '16 at 18:44
  • in this case `print middle` would print 3 so the middle item in the list – Ali Jun 30 '16 at 18:45
  • middle_index = len(alist) // 2 mid_value = alist[middle_index] – IronMaiden Aug 20 '19 at 02:36
  • I believe a simple `aList[len(aList)//2]` should do the job. – Aamir Jamal Dec 27 '19 at 15:00
  • 3
    This question is currently marked as a duplicate of another post, I believe in error. The post linked is about finding the median value of a list of numbers, but the post here is about finding the middle index of a list. These are semantically different questions, and as such I believe this question should be reopened. – CopOnTheRun May 13 '21 at 04:51
  • @AamirJamal is ok, but if you may have empty lists, try ```middle = aList[len(aList)//2] if aList else None``` – Konchog Dec 21 '21 at 11:25

4 Answers4

22

Something like this would do:

aList = [1,2,3,4,5]
#minus 1 because the first element is index 0
middleIndex = (len(aList) - 1)/2
print middleIndex
print aList[middleIndex]
heinst
  • 8,520
  • 7
  • 41
  • 77
14

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

Kyle Baker
  • 3,424
  • 2
  • 23
  • 33
  • 3
    Don't shadow the `list()` built-in – kylieCatt Jun 30 '16 at 18:54
  • 2
    What is `input_list[middle - .5]` supposed to do? – khelwood Jun 30 '16 at 18:55
  • 1
    @khelwood: if we have a list with positions [0,1,2,3,4], then the len()/2 of it will return 2.5. We want index 2. So we subtract .5 to get its position. – Kyle Baker Jun 30 '16 at 18:56
  • @IanAuld, just wrote this up very quickly, primarily from javascript land where that's not a protected word. Updated appropriately. – Kyle Baker Jun 30 '16 at 18:58
  • @KyleBaker Why don't you try it and see if it works like that? – khelwood Jun 30 '16 at 18:58
  • 1
    Subtracting .5 from a number will definitely give you a `float`, not an `int`, and you can't use a `float` as a list index. – khelwood Jun 30 '16 at 19:00
  • thanks @khelwood. Updated. `[1,2,3,4,5].length / 2` returns 2.5 in javascript, was not expecting 2 in python. edit: aaand just realized you can't use floats as indexes. Sorry, python is still relatively new to me. – Kyle Baker Jun 30 '16 at 19:05
  • This answer would make a lot more sense if you got rid of all the floats and the casts to and from `float`. – khelwood Jul 03 '16 at 16:23
  • 1
    The above logic doesn't work for all the even number list like [1,2,3,4,5,6] Instead of middle % 2 != 0, if we change it to len(input_list) % 2 != 0 it works for all the even number of list – dpd Jul 02 '20 at 16:07
3

Take the length of the list, cut that in half and access whatever the list at that index point.

Martin Lear
  • 262
  • 1
  • 7
  • Ok so something like `s=len(aList)/2` then use s as the index on the list? – Ali Jun 30 '16 at 18:41
  • 1
    yes, but you need to figure out what to do with even lists. You also need to be aware of odd length lists. If 7, your middle would be at index 3 so you need to round down. – Martin Lear Jun 30 '16 at 18:49
0

Are you expecting something like

def findMiddle(list):
  l = len(list)
  if l/2:
    return (list[l/2-1]+list[l/2])/2.0
  else:
    return list[(l/2-1)/2]