I see this as a case of too much repetition, which is generally a signal that things can be simplified, starting with `len(sort)`

.

We also need to determine if the inputs are literal, or an object. If we are certain that the parameter is a list literal, then we don't need a copy of the list. We can sort the parameter list in place.

```
def median(sample_space):
n = len(sample_space) # take the length only once
if n < 1:
return 0 # or False for empty list
if n < 2:
return sample_space[0] # value in element is the median
sample_space.sort() # sort in place
```

In the above example we take two basic steps and the problem is nearly solved. The list is sorted in place, and the length is assigned to `n`

.

If `n`

is odd, our job is almost complete. We need only the integer quotient of `n / 2`

to determine the index of the median in the sorted list.

```
m = n / 2
if n % 2 != 0:
return sample_space[m]
```

Now we can work with `n`

being even. The median will be the average (mean) of the two middle elements. What are their indices? `m`

and `m-1`

` return float(sample_space[m] + sample_space[m-1]) / 2`

Putting it all together, we have eleven lines of simple code.

```
def median(sample_space):
n = len(sample_space)
if n < 1:
return 0
if n < 2:
return sample_space[0]
sample_space.sort()
m = n / 2
if n % 2 != 0:
return sample_space[m]
return float(sample_space[m] + sample_space[m-1]) / 2
```

The above not meant as a solution, only an example of how things can be kept simple.