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

Answer -7

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?