I’m having trouble understanding why the correct code for this exercise functions the way it does and was hoping someone can explain it to me.
if len(lst) % 2 == 0:
sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1] return sum / 2
print(middle_element([5, 2, -10, -4, 4, 5]))
I’m having trouble grasping why the code after “else” grabs the middle integer in lists with odd numbers of integers. I’m trying to translate it into English in my head, but it seems like it would be saying "divide the total amount of integers(5) in half and return the integer occupying that space, which doesn’t exist since it would be the integer at 2 1/2 space. Happy to elaborate further if this isn’t making much sense. Thanks in advance!