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


Groups > comp.lang.python > #34259 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2012-12-05 08:36 +1100
Last post2012-12-05 08:36 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromChris Angelico <rosuav@gmail.com>
Date2012-12-05 08:36 +1100
SubjectRe: assign only first few items of a tuple/list
Message-ID<mailman.479.1354657005.29569.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web