Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Chris Angelico rosuav at gmail.com
Mon Mar 24 08:53:12 EDT 2014


On Mon, Mar 24, 2014 at 11:36 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> def get_oneth_element_index(item):
>>     return item[1].index
>> L.sort(key=get_oneth_element_index)
>>
>> Or do you just shortcut the whole thing by inlining it?
>>
>> L.sort(key=lambda item:item[1].index)
>
> I still prefer the "def" variant. It even allows you to clarify the
> meaning of the tuple slot by using a nicer name.

It's the index of element 1. What more do you need to know? Is it
actually any help to give that a name? All you gain is a chance for
the name, the purpose, and the functionality to come into
disagreement.

ChrisA



More information about the Python-list mailing list