What is this doing in the Python lambda function?
<p>In the following Python script where "aDict" is a dictionary, what does "_: _[0]" do in the lambda function?</p>

<pre><code>sorted(aDict.items(), key=lambda _: _[0])
</code></pre>

<p>In Python, lambda is used to create an anonymous function. The first underscore in your example is simply the argument to the lambda function. After the colon (i.e. function signature), the <code>_[0]</code> retrieves the first element of the variable <code>_</code>.</p>

<p>Admittedly, this can be confusing; the lambda component of your example could be re-written as <code>lambda x: x[0]</code> with the same result. Conventionally, though, underscore variable names in Python are used for "throwaway variables". In this case, it implies that the only thing we care about in each dictionary item is the key. Nuanced to a fault, perhaps.</p>

python sorting lambda key