What does the code zip( *sorted( zip(units, errors) ) ) do?
<p>For my application <code>units</code> and <code>errors</code> are always lists of numerical values. I tried googling what each part does and figured out the firs part of zip. It seems </p>

<pre><code>   ziped_list = zip(units, errors)
</code></pre>

<p>simply pairs the units and errors to produce a list as <code>[...,(unit, error),...]</code>. Then Its passed to sorted which sorts the elements. Since I did not provide the argument for <code>key</code>, then it compares the elements directly as the documentation implies:</p>

<blockquote>
  <p>The default value is None (compare the elements directly).</p>
</blockquote>

<p>Since the <code>ziped_list</code> is a list of tuples of integers, then it seems that it makes a comparison between tuples directly. From a small example in my terminal (python 3) it seems it compares based on the first element (even though the documentation implies the comparison is element wise):</p>

<pre><code>&gt;&gt;&gt; (1,None) &lt; (2,None)
True
&gt;&gt;&gt; (2,None) &lt; (1,None)
False
</code></pre>

<p>The last bit the unpacking and then zip still remain a mystery and I have not been  able to figure out what they do. I understand that <code>*</code> unpacks to positional argument but doing <code>*</code> doesn't let me see exactly what its doing if I try it in the command line. What further confuses me is why <code>zip</code> requires to be passed as argument an unpacked list such as <code>*sorted</code> if it already takes as an argument <code>zip(*iterable)</code> a variable called iterable. It just seems confusing (to me) why we would need to unpack something that just allows as input a list of iterables.</p>

<p>If you don't unpack list, then pass to argument as one element, so zip can't aggregates elements from each of the iterables.
For example:</p>

<pre><code>a = [3, 2, 1,]
b = ['a', 'b', 'c']
ret = zip(a, b)
the_list = sorted(ret)  
the_list &gt;&gt; [(1, 'c'), (2, 'b'), (3, 'a')]
</code></pre>

<p><code>zip(*the_list)</code> is equal to <code>zip((1, 'c'), (2, 'b'), (3, 'a'))</code></p>

<p>output : <code>[(1, 2, 3), ('c', 'b', 'a')]</code></p>

<p>If you just use <code>zip(the_list)</code> is equal to <code>zip([(1, 'c'), (2, 'b'), (3, 'a')],)</code></p>

<p>output: <code>[((1, 'c'),), ((2, 'b'),), ((3, 'a'),)]</code></p>

<p>You can also see <a href="http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters">What does ** (double star) and * (star) do for Python parameters?</a></p>

python