Sorry this seems strange to me that no one has asked here on this exercise. When trying to get the middle element should the list be sorted or unsorted. The first solution I came to was to use the median function from the statistics module (which implicitly sorts the list) and I get a value of 3.0 if instead we should leave the list unsorted the correct answer should be -7.0. It would be nice to have a correct answer/output on the lesson to make clear which is the desired method for this problem.

# Is middle different than median?

The list should be left as it is, unsorted, and then follow the directions as pertain to odd or even length.

This lesson is similar to the median, which applies only to a sorted list. It is not the median we seek, only the middle.

Thanks, a note to that effect would be nice in the lesson.

The only purpose that would serve would be to further assumptions that something is expected that is not asked for. The instructions say nothing of median, and are very clear about what is expected… Find the middle element(s) of a list, any list. We should not read anything else into it.

The reason I mentioned it is because when I was confused about how to solve the problem and when I searched google for finding the middle element of a list, the median function was the most common response. I’m sorry if I’ve come across as rude or presumptive. I don’t mean to I was just trying to think of others who might encounter the same confusion. Thank you for your time and assistance.

It’s got nothing to do with rudeness. You’re told you’re looking in the wrong place. This is neutral.

It’s only about cutting out your guesswork. @mtf is able to read what’s supposed to happen, how does he manage to do that? That’s the same thing you should be doing. In this case, the instructions say the middle value, that’s sufficient information, is it not? Three trees stand in a row. Cut down the middle one. Which one do you cut down? You’re obviously not going to move them around first, I’m sure you agree. Reading instructions sounds silly and even insulting. That’s what you missed, read them. Turns out this is hard (no, that’s not sarcasm) and it also turns out that it is much easier to do things when you know what you mean to do, making it very important/useful to get a super firm idea on what’s supposed to happen before you start doing anything at all.

Why this doesnt work? the code seems good to me but i get an error at the last line on return idedntation is correct though.

def middle_element(lst):

if len(lst) % 2 == 0:

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

return sum / 2

else:

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

for len(lst) % 2 ==1 : the middle element has the index of n/2 -1

and for len(lst)%2 ==0 there are two middle elements with value of n/2 -1 and n/2-1+1=n/2 So basically (n/2) and (n/2 )-1

A return statement must contain an expression (value) not a statement (assignment). What happens when we remove `sum =`

from that line?