FAQ: Code Challenge: Lists - Middle Item

Ah! Wasn’t aware of that operator. Thanks!

Would be worth the ten minutes or so it will take to research it further while it is fresh in your mind.

floor division

It’s important to note that floats have floors, too, and if one or both operands are a float, the result will be a float.

10.3 // 2  =>  5.0

Only when both operands are integer, will the quotient be an integer.

2 Likes

I couldn’t remember how to fool python into accounting for odd or even (that was a few excercises ago), so I hit the “see solution” button. It wrote this:

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

and when I hit solve it printed out: -7.0

I thought the program was supposed to show the middle 2 elements when even. -7.0 isn’t even the middle 2 added or subtracted from each other. So what the H? Did I fully misunderstand the exercise, or was the solution wrong?

Hello, @bit2995930909, and welcome to the forums.

It would seem you misunderstood. The exercise directs us to return the average of the two middle values when there are an even number of values.

image

We don’t attempt to fool Python. We just write code. We write it, and the computer executes what we wrote. :wink:

Ok, I missed the avg line. Thank you. Though I do feel like I’m trying to fool something that can add but doesn’t know what an odd number is. That is a joke. I mean, my joke, not the program.

1 Like

BTW, using floats for doing integer math is very strange. You’re using a more complicated type to represent something that your simple (and much nicer) type is perfectly able to do. If you do integer division you end up with an integer and discard the remainder.

The only time floats should be involved in anything you do, even as an intermediary step, is if you’re approximating something.

(A common mistake is to think that floats are for representing decimal values and that the numbers will work out nicely like those seen in grade school. That’s not what float is or does.)

can someone tell me why this returns -9?

#Write your function here
def middle_element(lst):
if len(lst) % 2 == 0:
return lst[(len(lst) // 2)] + lst[(len(lst) // 2) -1] // 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]))

Please, if you will, explain the logic here.

Hello!

In the index2 variable I’m storing the index to the “left” of the “middle” of the list.

I first find the length of the list with len(), then divide that by 2 to get the position/index to the “right” of the “middle.” I then subtract 1 to move to the left of the middle since we found the other position in the variable “index1.”

Python returns answers from division as floats, I believe, and since I can’t pass through a float into a list as an index, I have to wrap it in the int() function to make it an integer.

I hope this makes sense! It took me a little bit to figure this one out!

2 Likes

It makes perfect sense. Well done. TBH, I just wanted to test if you worked this out yourself. It certainly makes refactoring easier if we worked out and wrote the code ourselves.

For your own practice, see if you can reduce this function to a single return statement. It may not be today, but do come back when you are looking to review and practice.

Hello. I would like to know what does “int” mean a solution for this challenge:

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

I don’t remember working or learning with “int” before in this Python course and I solved it without it but I would be thankful if someone can explain what it stands for and what are it’s uses.

int is a Python data type to represent integer number type. It is a class with the constructor, int().

print (int(2.45))    #  2   =>  cast a float to an int

print (type(2))    #  <class 'int'>

In the solution I posted above is int there to convert float to integer?

Yes, that is because when two numbers are divided, even though they be integers themselves, the quotient is a float (Python 3) and we cannot use a float as an index.

Aside

Your code example above has a lot of repetition. len(lst) four times, and, lst[int(len(lst)/2)] three times. We can simplify and pretty up our function by using intermediate variables to cache the results in advance.

def middle_element(lst):
    n = len(lst)
    m = int(n / 2)

Now n and m are both integers and we can put that arithmetic to bed. For the rest of the code…

    if n % 2:
        return lst[m]
    return (lst[m] + lst[m - 1]) / 2
1 Like

All this makes sense… but why does it give me the index to the ‘right’ of the middle? Does this have something to do with rounding a float value etc?