Python beginner - Sorting tuples using lambda functions
<p>I'm going through the tutorial intro for Python and I'm stuck on understanding a piece of code. This is from section 4.7.5 of the tutorial.</p>

<pre><code>pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
pairs
</code></pre>

<p>This bit of code returns </p>

<pre><code>[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
</code></pre>

<p>So in line one, it defines pairs with a list of different tuples. I get that.
Line two is where I'm completely thrown off and I've done quite a bit of messing around with it to try to understand what's happening.</p>

<p>I've found that sort() is applying a built-in function to the variable pairs, and presumably it is sorting it per the instructions I'm giving it.</p>

<p>The sort() function requires a key, and key has to be defined using a function, hence the use of lambda. I think all of this is correct, but I may be way off here.</p>

<p>Lambda defines a new parameter, "pair" on the left side of the colon, and on the right side is the calculation to define the return value of the lambda function.</p>

<p>That's where I'm thrown off. What does "pair[1]" do? What is its affect on the "pair" on the left side of the colon? </p>

<p>What value does it return? I can't seem to get it to return any sort of value outside of coding it just like this.</p>

<p>I'm guessing that it somehow points to a specific tuple and sorts it based on repositioning that, but I'm not sure of the logic behind that. </p>

<p>Can anyone explain this for me? Thank you.</p>

<p>A <code>lambda</code> is a simplified function, using only an expression.</p>

<p>Any lambda can be written as a function, by adding in a <code>return</code> before the expression, so <code>lambda pair: pair[1]</code> becomes:</p>

<pre><code>def lambda_function(pair): return pair[1]
</code></pre>

<p>So the lambda in the <code>list.sort()</code> call here returns the <em>second</em> element of each sequence that is passed in (Python indexes start at <code>0</code>).</p>

<p>You can make this visible by assigning the lambda to a variable, say, <code>key</code>:</p>

<pre><code>&gt;&gt;&gt; pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
&gt;&gt;&gt; key = lambda pair: pair[1]
&gt;&gt;&gt; key(pairs[0])
'one'
&gt;&gt;&gt; key(pairs[1])
'two'
</code></pre>

<p>The <code>list.sort()</code> method uses the output of these calls (once for each element in the list) to sort the elements. So for the 4 elements, the function returns <code>'one'</code>, <code>'two'</code>, <code>'three'</code>, and <code>'four'</code>, and the 4 tuples are then sorted purely on the lexicographical (alphabetical) ordering of those 4 strings. That order would be <code>'four'</code>, <code>'one'</code>, <code>'three'</code>, <code>'two'</code>, which is what you see reflected in the final sorted list.</p>

<p>This form of sorting-by-alternative-key is commonly called a <a href="https://en.wikipedia.org/wiki/Schwartzian_transform" rel="nofollow"><em>Schwartzian transform</em></a>, after Randal L. Schwartz, who popularised the technique in Perl.</p>

python sorting lambda