Hans Hans - 1 year ago 78
Python Question

What is this doing in the Python lambda function?

In the following Python script where "aDict" is a dictionary, what does "_: _[0]" do in the lambda function?

sorted(aDict.items(), key=lambda _: _[0])

Answer Source

Lets pick that apart.

1) Suppose you have a dict, di:

di={'one': 1, 'two': 2, 'three': 3}

2) Now suppose you want each of its key, value pairs:

 >>> di.items()
 [('three', 3), ('two', 2), ('one', 1)]

3) Now you want to sort them (since dicts are unordered):

>>> sorted(di.items())
[('one', 1), ('three', 3), ('two', 2)]

Notice that the tuples are sorted lexicographically -- by the text in the first element of the tuple. This is a equivalent to the t[0] of a series of tuples.

Suppose you wanted it sorted by the number instead. You would you use a key function:

>>> sorted(di.items(), key=lambda t: t[1])
[('one', 1), ('two', 2), ('three', 3)]

The statement you have sorted(aDict.items(), key=lambda _: _[0]) is just using _ as a variable name. It also does nothing, since aDict.items() produces tuples and if you did not use a key it sorts by the first element of the tuple anyway. The key function in your example is completely useless.

There might be a use case for the form (other than for tuples) to consider. If you had strings instead, then you would be sorting by the first character and ignoring the rest:

>>> li=['car','auto','aardvark', 'arizona']
>>> sorted(li, key=lambda c:c[0])
['auto', 'aardvark', 'arizona', 'car']


>>> sorted(li)
['aardvark', 'arizona', 'auto', 'car']

I still would not use _ in the lambda however. The use of _ is for a throway variable that has minimal chance of side-effects. Python has namespaces that mostly makes that worry not a real worry.


>>> c=22
>>> sorted(li, key=lambda c:c[0])
['auto', 'aardvark', 'arizona', 'car']
>>> c

The value of c is preserved because of the local namespace inside the lambda.

However (under Python 2.x but not Python 3.x) this can be a problem:

>>> c=22
>>> [c for c in '123']
['1', '2', '3']
>>> c

So the (light) convention became using _ for a variable either in the case of a list comprehension or a tuple expansion, etc where you worry less about trampling on one of your names. The message is: If it is named _, I don't really care about it except right here...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download