Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '[1,': 0.09; 'function,': 0.09; 'modifies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'def': 0.12; 'in- place': 0.16; 'logiciel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:make': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; '---': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'est': 0.30; '>>>>': 0.31; 'but': 0.35; 'to:addr:python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'received:62': 0.63; 'protection': 0.63; 'skip:n 10': 0.64; 'virus': 0.65; 'antivirus': 0.68; 'courrier': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Boris Borcic Subject: Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ? Date: Thu, 10 Apr 2014 16:14:55 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: cust.static.62-202-12-176.swisscomdata.ch User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0 SeaMonkey/2.25 In-Reply-To: X-Antivirus: avast! (VPS 140410-0, 10.04.2014), Outbound message X-Antivirus-Status: Clean 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: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397139309 news.xs4all.nl 2971 [2001:888:2000:d::a6]:41204 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70039 Rustom Mody wrote: >>>> def fl1(l): return [y for x in l for y in x] > > > # recursive flatten >>>> def fr(l): > ... if not isinstance(l,list): return [l] > ... return fl1([fr(x) for x in l]) For a short non-recursive procedure - not a function, modifies L in-place b= ut none of its sublists. >>> def flatten(L) : =2E... for k,_ in enumerate(L) : =2E... while isinstance(L[k],list) : =2E... L[k:k+1]=3DL[k] flattens L to any depth, eg >>> L=3D[1,[2,[3,4]],5,6,[[7],8]] >>> flatten(L) >>> L [1, 2, 3, 4, 5, 6, 7, 8] --- Ce courrier =E9lectronique ne contient aucun virus ou logiciel malveillant = parce que la protection avast! Antivirus est active. http://www.avast.com