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

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Mar 24 10:21:02 EDT 2014


On 24/03/2014 12:36, Marko Rauhamaa wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> Similarly, there are plenty of cases where a nameless function is MUCH
>> clearer than breaking it out into a separate def and then using the
>> name once. Do you name the function for what it does internally?
>>
>> def get_oneth_element_index(item):
>>      return item[1].index
>> L.sort(key=get_oneth_element_index)
>>
>> Or for how you're using it?
>>
>> def keyfunc(item):
>>      return item[1].index
>> L.sort(key=keyfunc)
>>
>> 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.
>

Each to their own.  Here give me the lambda version any day of the week.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com





More information about the Python-list mailing list