hi, everyone. I need you to help here. I got stuck in following,
below a solution to an advanced python code challenges to find the Middle item
Find Middle item
Create a function called middle_element
that has one parameter named lst
.
If there are an odd number of elements in lst
, the function should return the middle element. If there are an even number of elements, the function should return the average of the middle two elements.
#Write your function here
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]))
I could not understand this line:
sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
plz explain to me