Welcome to the Get Help category!
Hello, Question 5 of https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/articles/advanced-python-code-challenges-lists
Is there a better/smoother way of doing this ? Thanks
#Write your function here
from statistics import mean
def middle_element(lst):
if len(lst) % 2 == 0:
return mean(lst[(len(lst)-1)//2:(len(lst)+2)//2])
elif len(lst):
return lst[int(len(lst)/2)]
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))