Advanced Python Code Challenges: Lists exercise 5


I would appreciate it if someone could breakdown the answer in exercise 5, as i’m struggling to translate how the code and why it is written in that way to reach the answer.

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

#Uncomment the line below when your function is done

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

Have you seen this discussion/explanation in the search results?

Or this:

I hope that helps. :slight_smile: