# MIDDLE ITEM - Understanding Solution

Middle Item

Corret Code:

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

print(middle_element([5, 2, -10, -4, 4, 5]))

I don’t understand certain parts of code and I was hoping someone would be able to clear it up for me.

if len(lst) % 2 == 0:
*Here we check the size of the list, if there are no leftover numbers after (%) then we would know the list is equal, is this correct?

sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
*would it look like this:
lst[int(len(-14)/2)] + lst[int(len(-14)/2) - 1]? if so, why should we -1 in the second equation?

Please have a look at the following as unformatted code is very difficult for others to read - How do I format code in my posts?

I’m not sure where the `-14` comes from. What does the `len` function do?

What does `int(len(lst)/2)` evaluates to for the example list of `lst = [5, 2, -10, -4, 4, 5]`, if you’re not sure then print it.

If you also consider what the middle element is for an even list then it should be a little clearer.