Median of three, pivot
<p>I'm looking for the <strong>median of three</strong>, using this for a pivot in a QuickSort. I would not like to import any statistics library because I believe it creates a bit of overhead which I would like to reduce as much as possible.</p>

<pre><code>def median(num_list):

if (num_list[0] &gt; num_list[len(num_list) - 1]) and (num_list[0] &lt; num_list[int(len(num_list)//2)]):
    return num_list[0]
elif (num_list[int(len(num_list)//2)] &gt; num_list[len(num_list) - 1]) and (num_list[0] &gt; num_list[int(len(num_list)//2)]):
    return num_list[int(len(num_list)//2)]
else:
    return num_list[len(num_list) - 1]
</code></pre>

<p>this seems to be returning the last else statement every time, I'm stumped...</p>

<p>Let Python do the work for you. Sort the three elements, then return the middle one.</p>

<pre><code>def median(num_list):
    return sorted([num_list[0], num_list[len(num_list) // 2], num_list[-1]])[1]
</code></pre>

python quicksort