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 ( ) below!
Agree with a comment or answer? 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 !
mtf
Split this topic
November 6, 2018, 3:16am
2
4 posts were split to a new topic: Why the ‘list’ command?
mtf
Split this topic
November 6, 2018, 3:13am
6
2 posts were split to a new topic: Why are the preceding lst required?
mtf
Split this topic
November 6, 2018, 3:10am
8
mtf
Split this topic
January 6, 2019, 7:05pm
9
2 posts were split to a new topic: Spoiler
catower
Split this topic
July 18, 2019, 8:37pm
10
8 posts were split to a new topic: Is middle different than median?
mtf
Split this topic
January 26, 2019, 1:36pm
15
2 posts were split to a new topic: Flaming troll
catower
Split this topic
July 18, 2019, 8:58pm
18
catower
Split this topic
August 14, 2019, 5:02pm
20
catower
Split this topic
August 14, 2019, 5:04pm
25
2 posts were merged into an existing topic: List indexes cannot have floats [solved]
catower
Split this topic
August 14, 2019, 5:06pm
27
catower
Split this topic
August 14, 2019, 5:07pm
29
3 posts were merged into an existing topic: List indexes cannot have floats [solved]
catower
Split this topic
August 14, 2019, 5:11pm
32
catower
Split this topic
August 14, 2019, 5:32pm
39
2 posts were merged into an existing topic: List indexes cannot have floats [solved]
catower
Split this topic
August 14, 2019, 5:39pm
42
catower
Split this topic
August 14, 2019, 5:54pm
45
2 posts were merged into an existing topic: List indexes cannot have floats [solved]
catower
Split this topic
August 14, 2019, 6:03pm
47
catower
Split this topic
August 14, 2019, 6:05pm
50
4 posts were merged into an existing topic: List indexes cannot have floats [solved]
I solved this by making the two scenarios into variables; even and odd. Each variable finds their respective midpoints, which makes them easy to average and call in the control flow. There’s probably a more elegant way to solve this, though, haha.
def middle_element(lst):
even = int(len(lst)/2) - 1
odd = int(len(lst)/2)
if len(lst) % 2 != 0:
return lst[odd]
else:
return (lst[even]+lst[odd])/2
ionatan
October 1, 2019, 12:31am
53
Rather than doing floating point arithmetic and rounding later, stick with integer operations:
# don't:
int(a / b)
# do:
a // b
1 Like