FAQ: Code Challenge: Lists - Middle Item

#1

This community-built FAQ covers the “Middle Item” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Middle Item

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

split this topic #2

4 posts were split to a new topic: Why the ‘list’ command?

split this topic #6

2 posts were split to a new topic: Why are the preceding lst required?

Why are the preceding **lst** required?
split this topic #8

3 posts were split to a new topic: Why do I have to include the /2 in the int()?

split this topic #9

2 posts were split to a new topic: Spoiler

#10

Sorry this seems strange to me that no one has asked here on this exercise. When trying to get the middle element should the list be sorted or unsorted. The first solution I came to was to use the median function from the statistics module (which implicitly sorts the list) and I get a value of 3.0 if instead we should leave the list unsorted the correct answer should be -7.0. It would be nice to have a correct answer/output on the lesson to make clear which is the desired method for this problem.

#11

The list should be left as it is, unsorted, and then follow the directions as pertain to odd or even length.

This lesson is similar to the median, which applies only to a sorted list. It is not the median we seek, only the middle.

#12

Thanks, a note to that effect would be nice in the lesson.

#13

The only purpose that would serve would be to further assumptions that something is expected that is not asked for. The instructions say nothing of median, and are very clear about what is expected… Find the middle element(s) of a list, any list. We should not read anything else into it.

#14

The reason I mentioned it is because when I was confused about how to solve the problem and when I searched google for finding the middle element of a list, the median function was the most common response. I’m sorry if I’ve come across as rude or presumptive. I don’t mean to I was just trying to think of others who might encounter the same confusion. Thank you for your time and assistance.

1 Like
split this topic #15

2 posts were split to a new topic: Flaming troll

#17

It’s got nothing to do with rudeness. You’re told you’re looking in the wrong place. This is neutral.

It’s only about cutting out your guesswork. @mtf is able to read what’s supposed to happen, how does he manage to do that? That’s the same thing you should be doing. In this case, the instructions say the middle value, that’s sufficient information, is it not? Three trees stand in a row. Cut down the middle one. Which one do you cut down? You’re obviously not going to move them around first, I’m sure you agree. Reading instructions sounds silly and even insulting. That’s what you missed, read them. Turns out this is hard (no, that’s not sarcasm) and it also turns out that it is much easier to do things when you know what you mean to do, making it very important/useful to get a super firm idea on what’s supposed to happen before you start doing anything at all.

#18

Here a suggestion:

def middle_element(lst):
if len(lst) % 2 != 0:
return lst[int(len(lst)/2)]
else:
return (lst[int(len(lst)/2)-1] + lst[int(len(lst)/2)])/2

#19

Is there a better way to learn/practice python math functions where the code challenges are actually reflective of the syntax/examples that were in the lesson? Instead of crazy math problems that are not even is the same universe as the lessons given? Any recommendations? Suggestions? I’m looking for somewhere to gradually get good at these math functions.

4 Likes
#20

This code ran in Atom but will not run in the python text editor. Anyone know why?

def middle_element(lst):
if (len(lst) % 2) != 0:
return lst[((len(lst) - 1) / 2)]
if (len(lst) % 2) == 0:
return (lst[len(lst) / 2] + lst[(len(lst) / 2) - 1]) / 2

#21

TypeError: list indices must be integers or slices, not float

3.5.2 (default, Nov 12 2018, 13:43:14)

In Python 3, this will be a float, unlike Python 2. Try using floor division, // 2.

def middle_element(lst):
  if (len(lst) % 2) != 0:
    return lst[((len(lst) - 1) // 2)]
  if (len(lst) % 2) == 0:
    return (lst[len(lst) // 2] + lst[(len(lst) // 2) - 1]) / 2
#22

Why this doesnt work? the code seems good to me but i get an error at the last line on return idedntation is correct though.
def middle_element(lst):
if len(lst) % 2 == 0:
sum = (lst[int((len(lst)/2)-2)] + lst[int(len(lst)/2)]
return sum / 2
else:
return sum = lst[int((len(lst)/2))-1]

for len(lst) % 2 ==1 : the middle element has the index of n/2 -1
and for len(lst)%2 ==0 there are two middle elements with value of n/2 -1 and n/2-1+1=n/2 So basically (n/2) and (n/2 )-1

#23

A return statement must contain an expression (value) not a statement (assignment). What happens when we remove sum = from that line?

#24

My solution to this… I think, it’s a bit different aproach, maybe someone finds it helpful ^^

def middle_element(lst):
return (lst[int(len(lst)/2)]+lst[int(len(lst)/2-0.5)])/2

that’s all :slight_smile:

#25

def middle_element(lst):
if len(lst) % 2== 0:
average_middle_numbers = lst[len(lst)/3] + lst[len(lst)/2]
return average_middle_numbers / 2
elif len(lst) % 2 !=0:
return lst[(2*len(lst)-1)]

I get:
File “script.py”, line 5, in middle_element
average_middle_numbers = lst[len(lst)/3] + lst[len(lst)/2]
TypeError: list indices must be integers or slices, not float
By the way i fixed it by putting int() but I dont remember when to put the int() command. can someone make refresh knowledge?