Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!news2.euro.net!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:two': 0.07; '(use': 0.09; 'done?': 0.09; 'tuple': 0.09; 'elements,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "that'll": 0.16; 'wrote:': 0.16; 'subject:list': 0.18; 'header:In- Reply-To:1': 0.22; 'dictionary': 0.23; 'loop,': 0.23; 'pm,': 0.24; 'aug': 0.24; 'separate': 0.28; 'handled': 0.28; 'loop': 0.28; 'sat,': 0.28; 'lists': 0.28; 'message-id:@mail.gmail.com': 0.29; 'construct': 0.30; 'efficiently': 0.30; 'tuples': 0.30; 'subject:?': 0.31; 'list': 0.32; 'there': 0.33; 'to:addr:python- list': 0.33; '...': 0.34; 'subject:lists': 0.36; 'another': 0.37; 'but': 0.37; 'two': 0.37; 'think': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'under': 0.39; 'to:addr:python.org': 0.39; 'your': 0.61; 'order': 0.62; 'hood.': 0.84; 'technically': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=enxSrnvAJRHl0rrPCFD9jcc2WP0ZWP5Y5BENpFDxwEw=; b=pEbbGN3R16hRBmFsG4OU4I0xUE5evAwkRn2gIqCVfsL3bFsPV8Zi9sdf8ZStXRd69y RxytB13Gw2RhKtL3jJDgEhGLU6GHHo9C3sc/Y31cdOm4+7c/ATWjfBdxBiOTqvfUpM4U gxrO850vVjw/cp7UKW7S5jk8tTmN4lwvfxgFw= MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 6 Aug 2011 18:14:16 +0100 Subject: Re: how to separate a list into two lists? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312650859 news.xs4all.nl 23926 [2001:888:2000:d::a6]:37260 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10977 On Sat, Aug 6, 2011 at 6:07 PM, smith jack wrote: > if a list L is composed with tuple consists of two elements, that is > L = [(a1, b1), (a2, b2) ... (an, bn)] > > is there any simple way to divide this list into two separate lists , such that > L1 = [a1, a2... an] > L2=[b1,b2 ... bn] > > i do not want to use loop, any methods to make this done? One easy way is to use list comprehensions. Technically that'll involve a loop, but the loop is handled efficiently under the hood. L1 = [x[0] for x in L] L2 = [x[1] for x in L] Another way would be to construct a dictionary from your list of tuples and then use the keys() and values() of that dictionary to form your lists (use collections.OrderedDict if the order matters). But I think the list comps are the best way. ChrisA