Advanced Python Code Challenges: Lists exercise 5

https://www.codecademy.com/paths/finance-python/tracks/introduction-to-python-for-finance/modules/learn-python3-lists/articles/advanced-python-code-challenges-lists

Hi,

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

else:

return lst[int(len(lst)/2)]

#Uncomment the line below when your function is done

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

Hi,
Have you seen this discussion/explanation in the search results?
https://discuss.codecademy.com/t/what-does-int-mean/524414

Or this:
https://discuss.codecademy.com/t/middle-item/461827

I hope that helps. :slight_smile: