Middle item - explanation of solution! Python advanced exercises



Depressingly I can’t understand the solution for the even handling (which doesn’t fill me with confidence regarding my understanding so far!)

Solution given as:

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)]

Since the list is six elements long I would expect it to run the even option which is meant to return the average of the middle two elements which apparently is -7 and for the life of me I cannot understand how it is computing that from the sum. Any explanations for someone who apparently cannot even understand a solution would be great - to say please simplify your explanation would be an understatement!

Thank you in advance, I will go back to making cave drawings in the meantime.

@clawja, don’t worry. We all have those days.
As you mentioned, it should return the average of the middle two elements.

To get the average, we add the two middle elements together and then divide by 2.
So, start with the first step:
What is -10 + -4?

Heya @el_cocodrilo can you tell I was having a defeated day :weary:

Thank you very much for taking the time with this (so as far as I know) that would be -14

Right, so the sum of the two middle elements is -14. Now to get the average, you just divide by 2.
So what is -14 / 2?

1 Like

hehe I don’t think I need to go right back to maths basics its more breaking down the code into manageable chunks and not being blinded by brackets!

So you have assigned the variable “sum” to the equation lst
Open [ (because its a list
int - because you need to convert it to a number to divide it (?)
len(lst)/2 because you are getting the second of the middle numbers in the list
You then add that result and do the same to get the first number in the middle because you have deducted one
and then finally you are dividing the total of those two numbers to get the average

Is that correct?

Just about.

So you can think of it as

sum = lst[middle_index1] + lst[middle_index2]

That way, you know that the indexes for the two middle elements are what is inside the brackets.

So for middle_index1, we have int(len(lst)/2) — the length of the list, divided by 2.

For middle_index2 we have int(len(lst)/2) - 1 — the length of the list, divided by 2, minus 1.

For a list with a length of 6, middle_index1 is 3 and middle_index2 is 2:

sum = lst[3] + lst[2]

So why do we wrap the division in int()?
It’s because Python 3 defaults to float division rather than integer division. So, if we divide the length of the list (6 in this case) by two, we get a float:


What happens if we try to use a float as an index?

1 Like

You’re a hero I have notes on floats! (should have got my highlighter out!) thank you so much that makes it a lot clearer. I think I will spend my day off this week going over this section again and seeing if I find the challenges a bit more manageable. If you have any tips on learning this I’m all ears I suppose practice makes perfect but if there is anything I can do/read to make concepts sink in better please send advice my way!

Thank you so much for taking the time to go through this with me and the screenshots, it has really helped

1 Like

It’s not a tip for learning per se but the provided solution is a little messy. Calculating int(len(lst) / 2) just once and saving it as a variable could clean it up quite a bit.

1 Like