Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '[1,': 0.09; 'assumed': 0.09; 'function:': 0.09; 'oh,': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'creates': 0.14; '[2,': 0.16; 'benjamin': 0.16; 'iterator': 0.16; 'loop.': 0.16; 'starred': 0.16; 'unpack': 0.16; 'unpacking': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'convenient': 0.24; 'helper': 0.24; 'first,': 0.26; 'right.': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '"",': 0.31; 'file': 0.32; '(most': 0.33; 'checking': 0.33; "i'd": 0.34; 'problem': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'yield': 0.36; 'doing': 0.36; 'method': 0.36; 'subject:?': 0.36; 'list': 0.37; 'list.': 0.37; 'received:209': 0.37; 'rather': 0.38; 'recent': 0.39; 'skip:u 10': 0.60; 'new': 0.61; 'numbers': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'skip:n 10': 0.64; 'more': 0.64; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'oscar': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=Wx+o+x1WjzGKhtO+I89cMoJRlM2rBp4+KcMpZcxrXCQ=; b=puq4C1PkVRtMXAFYVRcjM9Z8iawFo1FotVVZecYJuLmXea2/DOvBy7526Nhn/lWQGo EAdNnlPoSMfJEaytmnOHYMGez3At++2ZAByynlDIfCP69t/LbUhN6PFoQ5aywzU4N2Xc lqtcD9ngpEaqkWpvy5Tz5xEZ/ASItKEfEXgpJ14wDkzH2r9LfN274znvV5yASsfmqxAR 7m95xWamFlM7BF3tvKk0ocNzKFMgHQ34th4CUp1mnwnGcy6WILirmJUlHaOg7rVInTUN xxWosP9oxNFv62/cDPjKai1a5hk/Pe15IjZpBYuqMEMAk54jEtEfSUI27Nsl1Ub6yZlj 6BuQ== X-Received: by 10.58.161.6 with SMTP id xo6mr23691332veb.50.1366756979472; Tue, 23 Apr 2013 15:42:59 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Tue, 23 Apr 2013 23:42:39 +0100 Subject: Re: Nested iteration? To: Joshua Landau Content-Type: text/plain; charset=ISO-8859-1 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366756982 news.xs4all.nl 2296 [2001:888:2000:d::a6]:41598 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44230 On 23 April 2013 22:41, Joshua Landau wrote: > On 23 April 2013 22:29, Oscar Benjamin wrote: >> >> I just thought I'd add that Python 3 has a convenient way to avoid >> this problem with next() which is to use the starred unpacking syntax: >> >> >>> numbers = [1, 2, 3, 4] >> >>> first, *numbers = numbers > > > That creates a new list every time. You'll not want that over > try-next-except if you're doing this in a loop, and on addition (if you were > talking in context) your method will exhaust the iterator in the outer loop. Oh, you're right. I'm not using Python 3 yet and I assumed without checking that it would be giving me an iterator rather than unpacking everything into a list. Then the best I can think of is a helper function: >>> def unpack(iterable, count): ... iterator = iter(iterable) ... for n in range(count): ... yield next(iterator) ... yield iterator ... >>> numbers = [1, 2, 3, 4] >>> first, numbers = unpack(numbers, 1) >>> first 1 >>> numbers >>> list(numbers) [2, 3, 4] >>> first, numbers = unpack([], 1) Traceback (most recent call last): File "", line 1, in ValueError: need more than 0 values to unpack Oscar