Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.064 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'follows.': 0.09; 'def': 0.12; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'itertools': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'subject:Converting': 0.16; 'sublist': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'looks': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'subject:list': 0.30; 'easier': 0.31; 'lists': 0.32; 'figure': 0.32; 'convert': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'header :Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'email name:steve': 0.84; 'how.': 0.84; 'recursion:': 0.84; 'reply- to:addr:python.org': 0.84; 'lists:': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=MmqUQtAeV-wA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=F6cBLS3UKL8A:10 a=vvIjZZEgAAAA:8 a=qdX4a_K7QF1CPaQWsvcA:9 a=ohOPyMJI_8euYQsX:21 a=_z6pYjemdcLRt4ft:21 a=wPNLvfGTeEIA:10 a=dKVaqlLMqBEA:10 X-AUTH: mrabarnett:2500 Date: Tue, 23 Jul 2013 23:52:21 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Converting a list of lists to a single list References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374619944 news.xs4all.nl 15905 [2001:888:2000:d::a6]:41569 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51107 On 23/07/2013 22:52, 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. > > 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] > > An easier way to visualize the pattern I want is as a tree. > > 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 > Using recursion: def tree_list(items): if len(items) == 1: return items[0] sublist = tree_list(items[1 : ]) result = [] for item in items[0]: result.append(item) result.extend(sublist) return result items = [["A0","A1","A2"], ["B0","B1","B2"], ["C0","C1","C2"]] print(tree_list(items))