Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Iteration, while loop, and for loop Date: Tue, 28 Jun 2016 17:58:52 +0000 (UTC) Lines: 39 Message-ID: References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> X-Trace: news.uni-berlin.de fu2fXlo0cd7/TGcadm1CxQCV4SjseYIGRf5kPo0d1hTw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'bytes.': 0.07; 'data:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:qwest.net': 0.09; 'subject:while': 0.09; ':-)': 0.12; 'things.': 0.15; '"for"': 0.16; '"while"': 0.16; 'iterated': 0.16; 'len': 0.16; 'loops': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'seems': 0.23; 'tim': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sequence': 0.27; 'chase': 0.29; 'queue': 0.29; 'yields': 0.29; "d'aprano": 0.33; 'steven': 0.33; 'wrap': 0.33; 'similar': 0.33; 'that,': 0.34; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'things': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'telling': 0.61; 'more': 0.63; 'great': 0.63; 'done:': 0.84; 'edwards': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net User-Agent: slrn/1.0.2 (Linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <577295b3$0$1598$c3e8da3$5496439d@news.astraweb.com> <20160628122308.4578d876@bigbox.christie.dr> Xref: csiph.com comp.lang.python:110720 On 2016-06-28, Tim Chase wrote: > On 2016-06-29 01:20, Steven D'Aprano wrote: >> While loops are great for loops where you don't know how many >> iterations there will be but you do know that you want to keep >> going while some condition applies: >> >> while there is still work to be done: >> do some more work > > I find this particularly the case when the thing being iterated over > can be changed, such as a queue of things to process: > > items = deque() > items.append(root_node) > while items: > item = items.popleft() > process(item) > items.extend(item.children) Yep, I often do something similar when processing a block of data bytes comprising a sequence of "things" of varying number of bytes. data = read_a_blob_of_bytes() while data: #figure out how long the first "thing" is len = handle_thing(data[:len]) data = data[len:] > But then, if you wrap up your "while" loop as a generator that yields > things, you can then use it in a "for" loop which seems to me like > the Pythonic way to do things. :-) Yea, I keep telling myself that, but I never actually do it. -- Grant Edwards grant.b.edwards Yow! How do I get HOME? at gmail.com