Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assign': 0.07; 'finite': 0.09; 'subject:few': 0.09; 'terry': 0.09; 'variables,': 0.09; 'cc:addr:python-list': 0.10; 'dec': 0.15; '-tkc': 0.16; 'assigns': 0.16; 'folks,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterables': 0.16; 'lists).': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'suggestion.': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'work,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'am,': 0.27; '(such': 0.27; 'subject:/': 0.28; 'subject:list': 0.28; 'chris': 0.28; 'daniel': 0.30; "can't": 0.34; 'list': 0.35; 'something': 0.35; 'but': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'first': 0.61; 'anywhere.': 0.84; 'idiom': 0.84; 'picture,': 0.84; 'received:50.22': 0.84; 'does?': 0.91 Date: Tue, 04 Dec 2012 16:48:24 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Chris Angelico Subject: Re: assign only first few items of a tuple/list References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354662304 news.xs4all.nl 6891 [2001:888:2000:d::a6]:58867 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34263 On 12/04/12 15:36, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson > wrote: >> Hi folks, I swear I used to know this but can't find it anywhere. >> Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. >> I'd like to assign the first two items to two variables, something like, >> >> a, b, _ = x >> >> but the above will not work, of course, but what is the common idiom >> for this that does? > > Try this: > > a, b, *_ = x > > Assigns 1 to a, 2 to b, and [3,4,5] to _ Just to complete the picture, that's a Py3k thing. And it only works with finite iterables (such as lists). In 2.x, you have to use Terry Reedy's slicing suggestion. -tkc