title: lambda function in sorted dictionary list comprehension
body: <p>I have the following dictionary:</p>
  
  <pre><code>student_loan_portfolio = {
      'loan1': {'rate': .078, 'balance': 1000, 'payment': 100, 'prepayment': 0},
      'loan2': {'rate': .0645, 'balance': 10, 'payment': 5, 'prepayment': 0},
      'loan3': {'rate': .0871, 'balance': 250, 'payment': 60, 'prepayment': 0},
      'loan4': {'rate': .0842, 'balance': 200, 'payment': 37, 'prepayment': 0},
      'loan5': {'rate': .054, 'balance': 409, 'payment': 49, 'prepayment': 0},
      'loan6': {'rate': .055, 'balance': 350, 'payment': 50, 'prepayment': 0}
  }
  </code></pre>
  
  <p>I would like to iterate through the containing dictionary (with keys loan1 through loan6) in order of the key containing the dictionary with the highest 'rate' value in its respective nested dictionary. That is, I would like to iterate in order of loan3, loan4, loan1, loan2, loan6, loan5</p>
  
  <p>Thanks to @Jame Sharp the easiest way to do this that I know is:</p>
  
  <pre><code>for k,v in sorted(student_loan_portfolio.items(), key=lambda (k,v): v['rate'], reverse=True):
  </code></pre>
  
  <p>I am now reading about lambda and cannot really understand exactly how and why this works.
  First, v['rate'] I believe is returning the value of those dictionary keys.  But its seems like it should be some kind of syntax error.  what is v['rate'] referencing and what is the logic behind the syntax?</p>
  
  <p>On a related note, why, do we have to specify the inputs to the lambda function as a tuple?</p>
  
  <p>And how are the following cases different?</p>
  
  <pre><code>#1
  
  &gt;&gt;&gt;f = lambda x,y,z:x + y + z
  &gt;&gt;&gt;f(1,2,3)
  
  #6
  
  &gt;&gt;&gt;f = lambda (x,y,z): x + y + z
  &gt;&gt;&gt;f(1,2,3)
  
  Traceback (most recent call last):
    File "&lt;pyshell#48&gt;", line 1, in &lt;module&gt;
      f(1,2,3)
  TypeError: &lt;lambda&gt;() takes exactly 1 argument (3 given)
  </code></pre>
  
  <p>Thank you for the clarification.</p>