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 22:06:53 EDT 2014


On 25/03/2014 01:45, Steven D'Aprano wrote:
>
> (1) People who just want to get the job done, without learning a bunch of
> theory, *won't care* how their sort key function is written. They're
> looking for a recipe that they can copy and paste, and whether you write
> it like this:
>
>      data.sort(key=lambda item: item[1])
>
> or like this:
>
>      from operator import itemgetter
>      data.sort(key=itemgetter(1))
>
> or like this:
>
>      def sort_key(item):
>          return item[1]
>      data.sort(key=sort_key)
>
>
> *they won't care*. In fact, they'll probably prefer the first version,
> with lambda, because it is fewer lines to copy.
>

I'm firmly in this camp, practicality beats purity and all that.  I've 
used the first and second versions shown above as they happened to be in 
the recipes I was aquiring, I wouldn't contemplate the third.  That's 
just my mindset, which is what I love about Python, by pure luck it fits 
me like made to measure clothing.

-- 
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