Middle Element Function

Hi there,

Here is the code im referring to:

def middle_element(lst):
  if len(lst) % 2 == 0:
    sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
    return sum / 2
    return lst[int(len(lst)/2)]

#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))

This is the Codeacademy solution to finding the middle element. If the list has an even amount of elements, the middle two elements are averaged out. If the list has an odd amount of elements, it just returns the middle element.

I’m struggling to work out how the two middle elements are called, as on line 4 it says:

sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]

Looking at this, I can see that it grabs the size of the list, and halves it, which is 2 (the third element). then on the second part of the code, it grabs the length of the list, halves it, then minus’s 1 from the halved number, which is 1? Why is it calling 1 (the second element) rather than calling 3 (the fourth element)

The code runs fine so i know its an issue with my understanding rather than the code, but im struggling to understand why it uses - 1 rather than +1.

Could anyone tell me in laymen’s terms why - 1 is used please?



It might be a little nicer if these were the other way round but if you use the example list [5, 2, -10, -4, 4, 5] you’d have lst[int(len(lst)/2)]. If we start evaluating the epxression len(lst) == 6, then 6/2 either evaluates to 3 or 3.0 (float) depending on the python version. Calling int with this value would return integer 3.

So we have lst[3]. But the index starts at 0 so this the fourth element (integer -4 in the example). The other operand is identical but then 1 is taken away. That’d be lst[3]. So we now have a sum of the ‘middle two’ elements of our even list.

1 Like

Thanks for the help.

I think I understand now. On the first half of the ‘sum’, it halves the total list, which is 3. which is actually the 4th number in the list when referring to the list. This means that the second part of the equation equals 2, which is the 3rd number when referring to the list.

It seems simple now that I think about it. I guess this is just one of the intricacies of working with python, as I forgot that I had to convert that number back into what number it is on the list.

Also think it could be displayed a little bit easier, and flip the two sides of the equation around, that way the first half of the equation would be getting the ‘first’ of the two middle numbers, and the second half for the second middle number :stuck_out_tongue:. But now I realise what each side is doing, it is quite easy to understand.

Cheers for the help :slight_smile:

1 Like

I’d agree that statement in particular has a bit too much going on for my taste in addition to the unusual order. Especially since you’re basically evaluating an identical expression twice.

Perhaps something along the lines of idx = int(len(lst)/2) on the line beforehand would increase readability. Then the actual indexing is as simple as [idx - 1] and [idx] which is much more legible.