Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: assign only first few items of a tuple/list (http://www.velocityreviews.com/forums/t955125-re-assign-only-first-few-items-of-a-tuple-list.html)

Chris Angelico 12-04-2012 09:36 PM

Re: assign only first few items of a tuple/list
 
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


All times are GMT. The time now is 02:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.