Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69290

Re: Howto flaten a list of lists was (Explanation of this Python language feature)

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Howto flaten a list of lists was (Explanation of this Python language feature)
Date 2014-03-28 22:12 +0000
References <lh4r6h$sqr$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.8676.1396044784.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 28/03/2014 21:56, Mark H Harris wrote:
> On Fri, Mar 21, 2014 at 1:42 PM, vasudevram <vasudevram@gmail.com> wrote:
>  >> Can anyone - maybe one of the Python language core team, or someone
>  >> with knowledge of the internals of Python - can explain why this >>
> code works, and whether the different occurrences of the name x in >>
> the expression, are in different scopes or not? :
>  >>
>  >> x = [[1,2], [3,4], [5,6]]
>  >>     [x for x in x for x in x]
>
>  > I'll give this +1 for playfulness, and -2 for lack of clarity.
>
>  > I hope no one thinks this sort of thing is good to do in real-life code.

Strange, I thought Dan Stromberg wrote the above.
>
> No. This has to be a better way to flatten lists:
>
>  >>> from functools import reduce
>
>  >>> import operator as λ
>
>  >>> reduce(λ.add, l)
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
>

Why reinvent yet another way of flattening lists, particulary one that 
doesn't use the far more sensible:-

from operator import add

As for the stupid symbol that you're using, real programmers don't give 
a damn about such things, they prefer writing plain, simple, boring code 
that is easy to read.

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Howto flaten a list of lists was (Explanation of this Python language feature) Mark H Harris <harrismh777@gmail.com> - 2014-03-28 16:56 -0500
  Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-28 22:12 +0000
    Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark H Harris <harrismh777@gmail.com> - 2014-03-28 17:23 -0500
      Re: Howto flaten a list of lists was (Explanation of this Python language feature) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-29 02:33 +0000
        Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark H Harris <harrismh777@gmail.com> - 2014-03-28 22:04 -0500
          Re: Howto flaten a list of lists was (Explanation of this Python language feature) Rustom Mody <rustompmody@gmail.com> - 2014-03-28 20:21 -0700
            Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-29 15:46 +0000
          Re: Howto flaten a list of lists was (Explanation of this Python language feature) Chris Angelico <rosuav@gmail.com> - 2014-03-29 14:21 +1100
            Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark H Harris <harrismh777@gmail.com> - 2014-03-28 22:39 -0500
    Re: Howto flaten a list of lists was (Explanation of this Python language feature) Mark H Harris <harrismh777@gmail.com> - 2014-03-28 17:23 -0500
    Re: Howto flaten a list of lists was (Explanation of this Python language feature) mm0fmf <none@mailinator.com> - 2014-03-29 15:59 +0000

csiph-web