Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34259

Re: assign only first few items of a tuple/list

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'assign': 0.07; 'subject:few': 0.09; 'variables,': 0.09; 'dec': 0.15; 'assigns': 0.16; 'folks,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'work,': 0.22; "i'd": 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'am,': 0.27; 'received:209.85.210.46': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:/': 0.28; 'subject:list': 0.28; 'daniel': 0.30; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'two': 0.37; 'received:209': 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; 'does?': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=M58ssKbhZ1dPPGSM5qJ0NV9wIdosui393v2ENx2hKyc=; b=XO8RBnxkFcLwClNvnmH4VJb1yKVoZews4dCOZQLBRIC3kIufGO7PWa5kxEO7ov6Ojs 606Oys13qv1Jplb7RzAgYHuTfynuC+L+TqR2P1bocsAaxRfALbrKf/xQ2iQvLB9FnOaD TF3hBzZ/Ae8kHAgjiKaB4KODEkhI4LHSPRUS/ZgwEWFASUp8SR54kxbAe0DDmyGT13fj VhfFsypyq2nEuFgKbLStprZoK/zTpsjqnBqVeE3ffOyHJUNFp6HzOhL5cH3AKQ/M/0t2 oxZkeIfqf34BxDv0u4MuWRjjyOxtx/9quNIItENGMqYTsEXGpzlnV0wvfhlF3cA+ee9G 5O+w==
MIME-Version 1.0
In-Reply-To <CADjSo4TP03MTF_8Htn-N4xNV1zB08wTK7geLvxU0jYX_FXUqmg@mail.gmail.com>
References <CADjSo4TP03MTF_8Htn-N4xNV1zB08wTK7geLvxU0jYX_FXUqmg@mail.gmail.com>
Date Wed, 5 Dec 2012 08:36:41 +1100
Subject Re: assign only first few items of a tuple/list
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.479.1354657005.29569.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354657005 news.xs4all.nl 6905 [2001:888:2000:d::a6]:44102
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34259

Show key headers only | View raw


On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson
<fetchinson@googlemail.com> 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 _

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: assign only first few items of a tuple/list Chris Angelico <rosuav@gmail.com> - 2012-12-05 08:36 +1100

csiph-web