Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'skip:[ 20': 0.04; 'subject:Python': 0.06; '[1,': 0.09; 'dan': 0.09; 'subject:language': 0.09; 'received:10.20.200': 0.16; 'received:charter.net': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'code.': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'received:10.20': 0.33; 'subject: (': 0.35; "i'll": 0.36; 'list': 0.37; 'received:10': 0.37; 'message- id:@gmail.com': 0.38; 'subject:]': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'hope': 0.61; 'received:96': 0.65; 'lack': 0.78; 'subject:this': 0.83; 'real- life': 0.84; 'lists:': 0.91 X-Authority-Analysis: v=2.0 cv=Q7eKePKa c=1 sm=1 a=EBsYl1X+Lq2xj3QUCo3MmA==:17 a=zewopLiEtFcA:10 a=8uICfAjvTeIA:10 a=nDghuxUhq_wA:10 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=BbWEGnyQ8XcKhXzfd94A:9 a=QEXdDO2ut3YA:10 a=tJ3V6f3svowA:10 a=Fo4m9t2jTAgA:10 a=EBsYl1X+Lq2xj3QUCo3MmA==:117 Date: Fri, 28 Mar 2014 17:05:15 -0500 From: Mark H Harris User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 Newsgroups: comp.lang.python To: python-list@python.org Subject: To flatten a nested list was (Explanation of this Python language feature? [x for x in x for x in x] References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396044320 news.xs4all.nl 2899 [2001:888:2000:d::a6]:56981 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69289 On 3/27/14 6:45 PM, Dan Stromberg wrote: >> >> 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 >>> L = [[1,2,3],[4,5,6],[7],[8,9]] >>> import operator as λ >>> reduce(λ.add, L) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> marcus