Search code examples
pythonlistreturn-valuemedian

Understanding return statement in the function finding the median of numbers


I have this function:

def median(numbers):
    middle = int(len(numbers)/2)
    return numbers[middle]

print(median([4, 5, 6, 7, 8]))

The writing style of this line is a bit confusing for me:

return numbers[middle]

If I am not mistaken it returns the input numbers' order in a list.
Is there a different way to read this line?


Solution

  • numbers[middle] is slicing of the list, which returns the element at middle position. It returns a single element, which is at the middle index, starting from 0.