Advanced Lists Practice Help

I attempted the middle elements practice but I wasn’t able to get it and I don’t understand how the solution works.

This is what I tried originally.

#Write your function here
def middle_element(lst):
  for element in lst:
    if len(lst) % 2 == 0:
      middle = len(lst)/2
      return middle
    elif len(lst) % 2 != 0:
      middle1 = list[2]
      middle2 = list[-2]
      average = middle1 + middle2 /2
      return average

#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))

This is the solution and the prompt
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 understand the part where they checked in the length of the list was odd or even. I also see where I got confused between and odd and even list. If it was even then two elements would be left in the

My question is…what happened here??
sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]

well, the code should work for a list of any length. Your seems to work if there 6 elements, but what if there are 2? or 4? or 8? or 10? and so forth

you need to get the left and right middle values, which is what the code from the solution is doing. Getting the left and right middle values from the list (regardless of length)

You might be getting caught up in the code because it is a bit convoluted. Try to split it into steps to see how it works!

if len(lst) % 2 == 0:
    indexToMiddle = int(len(lst) / 2)
    leftNumber = lst[indexToMiddle - 1]
    rightNumber = lst[indexToMiddle] 
    sum = leftNumber + rightNumber
    return sum / 2

Due to the nature of a list that has an even amount of elements, like this:
[1, 2, 3, 4, 5, 6]
when you try to find the “middle”, your index will point to the right value of that middle because we start counting from zero (so 6 / 2 = 3, index 3 is to the right of the middle). That’s why you have to do a -1 for one of the numbers!

Ok I see. I knew it wasn’t always going to be a six element list but I didn’t know how to fix it to be general.

@irlfede

Ok so. When you used the square brackets it means you are getting the index?

So we’re using the index to find the elements in the middle list and adding them together
lst[3] & lst[2] gets -10 & -4 in this list
[5, 2, -10, -4, 4, 5]

Yup! The prompt asked you to return the average of the two middle numbers of the list has an even number of elements. And we access the values inside a list using the square brackets, like you said.

So your logic should be:

  1. Check if the list is even
  2. If it is, find the indices of the two elements in the middle (here we use len() so the solution can be general for any list size)
  3. Grab the values using the indices (square brackets!)
  4. Return average