Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!cs.uu.nl!news0.firedrake.org!news.nosignal.org!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'assign': 0.07; 'python': 0.09; 'notation': 0.09; 'subject:few': 0.09; 'terry': 0.09; 'variables,': 0.09; 'cc:addr:python-list': 0.10; 'dec': 0.15; 'assigns': 0.16; 'folks,': 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; 'subject:/': 0.28; 'subject:list': 0.28; 'chris': 0.28; 'points': 0.29; 'daniel': 0.30; "can't": 0.34; 'list': 0.35; 'pm,': 0.35; 'something': 0.35; 'but': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'first': 0.61; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'anywhere.': 0.84; 'idiom': 0.84; 'received:74.208.4.194': 0.84; 'does?': 0.91 Date: Tue, 04 Dec 2012 17:50:24 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 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-Provags-ID: V02:K0:hcbIqYqiEy1gNoPak6whaVZbTDEE2wAyMhsxhAxtnng Auqxh/2txPRvXeePzQdB35f8t6iL85ag24O8XiiPAMhh+H2Sw7 BybJNmI7InIo/1gNB3dRRUrzq2Z+FUiVlEGoHK+JAvZZeJTrpz hE/++cf3v37CxTI58hMgXVR8sp7jRtXbGlJSbnzj8FoV5rPAIH 1Tqiu4uWSJ3i2GWdYlzEMkoR/Yi+DIvkWxHF2qL3Q0utaB4PoJ Zrgj3s6AYcDOSv/qQig3n6WqcVmg/uuBNcDuY/yFTMU4VvO+YS iPhwHy/w+XZuKKGVwMvzad3nREj72pzkMtjLLvWoFbYdvpKew= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354661449 news.xs4all.nl 6894 [2001:888:2000:d::a6]:53851 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34262 On 12/04/2012 04: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 _ > If you're on Python 3.x. If you're on 2.x, you need to do a slice notation like Terry points out. -- DaveA