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>Using <code>min</code> and <code>max</code>:</p>

<pre><code>&gt;&gt;&gt; numlist = [21, 12, 16]
&gt;&gt;&gt; a, b, c = numlist
&gt;&gt;&gt; max(min(a,b), min(b,c), min(a,c))
16
&gt;&gt;&gt; 
</code></pre>

<hr>

<p>Going out on a limb - I have a functional streak so here is the itertools equivalent, even though it means importing a module</p>

<pre><code>&gt;&gt;&gt; import itertools
&gt;&gt;&gt; numlist = [21, 12, 16]
&gt;&gt;&gt; z = itertools.combinations(numlist, 2)
&gt;&gt;&gt; y = itertools.imap(min, z)
&gt;&gt;&gt; max(y)
16
</code></pre>

python quicksort