 # Help with understanding "Advanced Python Code Challenges: Lists #5 Question

Hello! I’ve recently started learning Python 3 via Codeacademy and it slowly has started to click. When I reached this section of the course, I became dumbfounded as I see Python being used in ways I don’t understand up to this point.

I just wanted to get help and clarification on why this is the solution, and how each part functions within the function. Here is the problem and solution:

#5 Middle element

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.

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

prints: -7

Hello @py2025681704 welcome to the forums. I will go step by step:

This creates the function (as I am sure you know).

This checks to see if the list’s length is even or odd. The `if` is an if statement. The `len(lst)` is a function that return the length of its parameter. The `%` is a modulo. It returns the remainder of a division sum. Therefore if the length of the list `len(lst)` divided by two `len(lst) % 2` gives no remainder `len(lst) % 2 == 0`, then execute the code in the `if`.

I will break this into parts, but the ultimate goal is to find the two middle elements of a list, add them together, and divide by two. So:

This is list indexing. What it is doing is saying: return the item at `int(len(lst)/2)` of the list `lst`. So. `int()` is a built in function that returns the integer version of a number. It always rounds down. For example:

``````a = int(3.2)#returns 3
b = int(3.5)#retruns 3
c = int(3.9999)#returns 3
d = int(4)#returns 4.
``````

The `len(lst)` returns the length of the list. The `/2` divides the result of `len(lst)` by two. This is where the `int()` comes in. You can’t index a float. Therefore, you round down to make the number an index.

This does the same thing as the code I explained above, with one difference. It takes the result of rounding down the length of the list divided by two, and then subtracts 1.

This comes back to here. We add the result of the two steps and save it to sum. You could also save each step in a variable:

``````lst = [1, 2, 3, 4, 5]
def length(lst):
a = lst[int(len(lst) / 2)]#returns 3. (The item at the second index.)-Remember, list indexes start at 0.
b = lst[int(len(lst) / 2) - 1]#returns 2.(The item at the first index)
``````

This returns the result of sum (above) divided by two.
Finally:

This happens if the length of the list is odd. It returns the item of the list `lst` that is at the position of the length of the list divided by two, then rounded down (see above).
So. How does this :`print(middle_element([5, 2, -10, -4, 4, 5]))` get negative seven?
Firstly it counts the number of elements: 6. Then it sees if that can be divided by two? Yes. It then breaks it down: what element is at the `int(len(lst) / 2)`? -4. (see above). It then takes the value of `int(len(lst)/2)` and subtracts 1. This gives two. Then it finds the item at list index two: -10. Finally, it adds them together. (-10 + -4 is -14). Lastly, for this part:

It returns -14 / 2. This gives -7.
I hope this helps!

2 Likes

Welcome. If possible always try to annotate any code you post to the forums which makes it easier for anyone reading your code to assist. You can do so using triple backquotes/grave accents as follows-
` ` `
{code goes here}
` ` `
It’s always helpful to break it up into consituent parts. Each part of this function has been used before in a lesson. Sticking them alotgether obviously makes the code much more complex but also allows a great deal more to be achieved.

The answer by @tera5288723178 is a good example of how you might break this down yourself in the future (then you can limit queries to specific lines and how they lead to the given outcome). Not only does this make it easier to reply to than a length of code but it should also help to make the process more clear to you yourself which is the main goal.
Happy coding!

3 Likes

Thank you for telling me as I wasn’t sure how to annotate lol. I’ve seen all these separately before, but didn’t know how technical I could get with mixing all of them together to achieve something that seems so simple. I’ve so much more to learn! Thank you again!

1 Like

Thank you so, so much for this detailed description! I really appreciate this immensely! I can get so frustrated when I think I’m doing good, and hit a wall where I feel terrible for not understanding this because the lessons make me feel like I should.

1 Like

Hello. Don’t worry about not understanding concepts; just slow down and rethink things. Take things at your own pace!

1 Like   1 Like