Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assign': 0.07; 'item.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:few': 0.09; 'terry': 0.09; 'variables,': 0.09; 'dec': 0.15; 'assigns': 0.16; 'folks,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'jan': 0.18; 'work,': 0.22; "i'd": 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'common': 0.26; 'am,': 0.27; 'separate': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:/': 0.28; 'subject:list': 0.28; 'chris': 0.28; 'daniel': 0.30; 'to:addr :python-list': 0.33; "can't": 0.34; 'list': 0.35; 'pm,': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'anywhere.': 0.84; 'idiom': 0.84; 'received:fios.verizon.net': 0.84; 'does?': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: assign only first few items of a tuple/list Date: Tue, 04 Dec 2012 17:18:25 -0500 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:16.0) Gecko/20121026 Thunderbird/16.0.2 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354659526 news.xs4all.nl 6853 [2001:888:2000:d::a6]:38558 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34260 On 12/4/2012 4:36 PM, 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 _ Or a, b = x[0:2]; depending on whether you do or do not want the remainder as a separate item. -- Terry Jan Reedy