# Median - How do I remove the brackets?

#1

median([1]) returned [1] instead of 1

So my code seems to be working, at least for the first list, but when I run it, it throws me the error. median([1]) returned [1] instead of 1

I think somewhere along the way this sort of thing was covered. But i forget where at.

My question, pretty much, is how to remove the brackets from what the function returns.

``````def median(x):
final = sorted(x)
if len(final) % 2 == 0:
while len(final) > 2:
final.remove[0]
final.remove[len(final)]
else:
while len(final) != 1:
final.remove[0]
final.remove[len(final)]
if len(final) == 2:
avg = final[0] + final[1]
avg = avg/2
final = avg
return final``````

#2

Your function is computing the median by attacking (supposedly) the left and right side, whittling them down.

``````def median(x):
final = sorted(x)
while len(final) != 1:
del final[0]
del final[len(final)+1]
return final``````

`````` >  median([3,5,7,9,7,6,4,3,7,6])
Traceback (most recent call last):
File "python", line 1, in <module>
File "python", line 5, in median
IndexError: list assignment index out of range``````

Of course, now you swapped out your code (which is a no-no, better to just delete your topic and start a new one).

New error message:

`````` >  median([3,5,7,9,7,6,4,3,7,6])
Traceback (most recent call last):
File "python", line 1, in <module>
File "python", line 5, in median
TypeError: 'builtin_function_or_method' object is not subscriptable
>``````

#3

Have you solved this, yet?

``````def median(x):
final = sorted(x)
if len(final) % 2 == 0:
while len(final) > 2:
final.remove(final[len(final) - 1])
final.remove(final[0])
return float(final[0] + final[1]) / 2
else:
while len(final) > 1:
final.remove(final[len(final) - 1])
final.remove(final[0])
return final[0]``````

``````def median(x):
x = sorted(x)
if len(x) % 2 == 0:
while len(x) > 2:
x.pop(len(x) - 1)
x.pop(0)
return float(x[0] + x[1]) / 2
else:
while len(x) > 1:
x.pop(len(x) - 1)
x.pop(0)
return x[0]``````

``````def median(x):
x = sorted(x)
n = len(x)
while len(x) > 1:
if len(x) == 2 and n % 2 == 0:
return float(x[0] + x[1]) / 2
x.pop(len(x) - 1)
x.pop(0)
return x[0]``````

#4

Not yet. I took a little break. I'm just starting yet. Sorry about replacing my code by the way. Should I put it back?

#5

No, I posted it in my reply so just keep your post as is. Thanks for asking.

If you study the spoiler examples I posted you may gain some insight into your approach and the built-ins used (as well as their syntax).

#6

Okay. Thanks for helping me out

#7

Final refactoringâ€¦

``````def median(x):
x = sorted(x)
n = not len(x) % 2
while len(x) > 1:
if len(x) == 2 and n:
return float(x[0] + x[1]) / 2
x.pop(0)
x.pop()
return x[0]
``````

#8

Wow!
Thanks for taking your time to help me out!

#9

Just be sure to think this through, the same way I did. Go back over this lesson and the code until you have a clear understanding of it. Don't just leave.

#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.