Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Mark H Harris Newsgroups: comp.lang.python Subject: How to flatten a list of lists was (Explanation of this Python language feature?) Date: Fri, 28 Mar 2014 17:00:27 -0500 Organization: Aioe.org NNTP Server Lines: 31 Message-ID: <5335F0FB.3040904@gmail.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> NNTP-Posting-Host: +K+LVGHWjDoy/+unkid0vA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.python:69286 On 3/27/14 6:45 PM, Dan Stromberg wrote: > On Fri, Mar 21, 2014 at 1:42 PM, vasudevram 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. > You might try this to flatten a list of lists: >>> from functools import reduce >>> import operator >>> import operator as λ >>> reduce(λ.add, l) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> marcus