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


Groups > comp.lang.python > #44230

Re: Nested iteration?

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 <oscar.j.benjamin@gmail.com>
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 <CAN1F8qXu7w=8YVWKvE2LU1etvyW86HS0AZSskpabXOZzLJCrgw@mail.gmail.com>
References <kl6a1f$k2l$1@panix2.panix.com> <CAPTjJmpKR8z7Cub1WaurmsuugNgCvPYFdh9WYqJd_1OTfpLw6Q@mail.gmail.com> <CALwzidk6qz4qHOFWYqv_cDEtjbhvS_kjG0vXJZmeB0dJ07doTQ@mail.gmail.com> <CAHVvXxROqnNt1bwvvQLNNUczGbFDUdZ7yu3=Om3DQHaekzZj-g@mail.gmail.com> <CAN1F8qXu7w=8YVWKvE2LU1etvyW86HS0AZSskpabXOZzLJCrgw@mail.gmail.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Tue, 23 Apr 2013 23:42:39 +0100
Subject Re: Nested iteration?
To Joshua Landau <joshua.landau.ws@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python <python-list@python.org>
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.1004.1366756982.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 23 April 2013 22:41, Joshua Landau <joshua.landau.ws@gmail.com> wrote:
> On 23 April 2013 22:29, Oscar Benjamin <oscar.j.benjamin@gmail.com> 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_iterator object at 0x24e1590>
>>> list(numbers)
[2, 3, 4]
>>> first, numbers = unpack([], 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack


Oscar

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


Thread

Nested iteration? roy@panix.com (Roy Smith) - 2013-04-23 11:40 -0400
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 17:05 +0100
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:05 -0600
  Re: Nested iteration? Peter Otten <__peter__@web.de> - 2013-04-23 18:15 +0200
  Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:21 +1000
  Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:35 +0000
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:30 -0600
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:39 -0600
  Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:42 +1000
    Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:53 +0000
  Re: Nested iteration? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 16:49 -0400
  Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:14 +0100
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 22:29 +0100
  Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:41 +0100
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 23:42 +0100

csiph-web