Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'follows.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; 'first:': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'skip:[ 40': 0.16; 'subject:Converting': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'passes': 0.24; 'looks': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'assert': 0.31; 'lists': 0.32; 'figure': 0.32; 'convert': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'yield': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:a 30': 0.61; 'received:173': 0.61; 'first': 0.61; '102': 0.84; 'email name:steve': 0.84; 'how.': 0.84; 'received:fios.verizon.net': 0.84; 'lists:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Converting a list of lists to a single list Date: Tue, 23 Jul 2013 19:02:44 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 In-Reply-To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374620579 news.xs4all.nl 15945 [2001:888:2000:d::a6]:49088 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51109 On 7/23/2013 5:52 PM, steve@divillo.com wrote: > I think that itertools may be able to do what I want but I have not > been able to figure out how. A recursive generator suffices. > I want to convert an arbitrary number of lists with an arbitrary > number of elements in each list into a single list as follows. > > Say I have three lists: > > [[A0,A1,A2], [B0,B1,B2] [C0,C1,C2]] > > I would like to convert those to a single list that looks like this: > > [A0,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2, > A1,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2, > A2,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2] def crossflat(lofl): if lofl: first = lofl.pop(0) for o in first: yield o yield from crossflat(lofl.copy()) A0, A1, A2 = 100, 101, 102 B0, B1, B2 = 10, 11, 12 C0, C1, C2 = 0, 1, 2 LL = [[A0, A1, A2], [B0, B1, B2], [C0, C1, C2]] cfLL = list(crossflat(LL)) print(cfLL) assert cfLL == [ A0, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2, A1, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2, A2, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2] passes -- Terry Jan Reedy