title: lambda function error: TypeError: 'function' object is not iterable
body: <p>I'm trying to execute the following <a href="https://github.com/DiceTechJobs/ConceptualSearch/blob/master/jupyter%20notebooks/Conceptual%20Search%20-%204.c%20Create%20Cluster%20Synonym%20Files.ipynb" rel="nofollow noreferrer">python notebook</a>:</p>
  
  <p>Them I came to the part where I have to use the function 'compute_cluster_similarities'.</p>
  
  <pre><code>def compute_cluster_similarities(kwds, kwd2id, vectors, lbl2centroid):
      kwd2cluster_sims = dict()
      for kwd in kwds:
          ix = kwd2id[kwd]
          nvec = vectors[ix]
          sims = []
  
          for lbl, centroid in lbl2centroid.items():
              cosine_sim = np.inner(nvec, centroid)
              sims.append((lbl,cosine_sim))
          sims = sorted(sims, key = lambda lbl,sim: -sim)
          kwd2cluster_sims[kwd] = sims
          if len(kwd2cluster_sims) % 1000 == 0:
              print("%i computed out of %i" % (len(kwd2cluster_sims), len(all_kwds)))
      return kwd2cluster_sims
  </code></pre>
  
  <p>And its returning the error:</p>
  
  <blockquote>
    <p>TypeError: () missing 1 required positional argument: 'sim'</p>
  </blockquote>
  
  <p>First of all, I'm still trying to understand this part of the lambda code. I learned what is the objective of the lambda instruction but couldn't understand what is the point of this line of code, like.. is it (implicity) returning 2 values (sims, key)?? What is being sorted?</p>
  
  <p>I think that this error is ocurring due the Python 3, but even if this was executed on Python 2, it doesn't make sense to me.
  It's very confusing for me... How can I solve this error? And please, give me some explanation about what it's going on, not just the fix.</p>
  
  <p>EDIT:
  I'm using pdb library to debug the code and I realized that this error was being returned by the 'sorted()' function. The original error is:</p>
  
  <blockquote>
    <p>*** TypeError: 'function' object is not iterable</p>
  </blockquote>
  
  <p>What I did:</p>
  
  <pre><code>        cosine_sim = np.inner(nvec, centroid)
          sims.append((lbl,cosine_sim))
      import pdb ; pdb.set_trace();
      sims = sorted(sims, key = lambda lbl,sim: -sim)
  </code></pre>
  
  <p>and them at the Pdb mode:</p>
  
  <pre><code>(Pdb) sims, key = lambda lbl,sim: -sim
  *** TypeError: 'function' object is not iterable
  </code></pre>