Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: unpacking first few items of iterable (http://www.velocityreviews.com/forums/t955470-re-unpacking-first-few-items-of-iterable.html)

MRAB 12-13-2012 08:09 PM

Re: unpacking first few items of iterable
 
On 2012-12-13 19:37, Daniel Fetchinson wrote:
> Hi folks, I swear I used to know this but can't find it anywhere:
>
> What's the standard idiom for unpacking the first few items of an
> iterable whose total length is unknown?
>
> Something like
>
> a, b, c, _ = myiterable
>
> where _ could eat up a variable number of items, in case I'm only
> interested in the first 3 items?
>

You could do this:

from itertools import islice

a, b, c = islice(myiterable, 3)



All times are GMT. The time now is 08:54 AM.

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