Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'nested': 0.07; 'executes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'resume.': 0.09; 'worked.': 0.09; 'jan': 0.12; 'anyway': 0.14; "'break'": 0.16; 'assignment?': 0.16; 'iterable': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'nest': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'statement.': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'possibly': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'rest': 0.29; 'am,': 0.29; 'code': 0.31; 'terminate': 0.31; 'file': 0.32; 'another': 0.32; 'quite': 0.32; 'not.': 0.33; 'could': 0.34; 'common': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'raising': 0.36; 'done': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'clear': 0.37; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'does': 0.39; 'aside': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'break': 0.61; 'today,': 0.61; 'received:173': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'different': 0.65; 'effectively': 0.66; 'smith': 0.68; 'legal': 0.71; '(probably': 0.84; 'clearer': 0.84; "else's": 0.84; 'idiom': 0.84; 'received:fios.verizon.net': 0.84; 'constitute': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: Nested iteration? Date: Tue, 23 Apr 2013 16:49:30 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366750170 news.xs4all.nl 2291 [2001:888:2000:d::a6]:34758 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44214 On 4/23/2013 11:40 AM, Roy Smith wrote: > In reviewing somebody else's code today, I found the following > construct (eliding some details): > > f = open(filename) > for line in f: > if re.search(pattern1, line): > outer_line = f.next() > for inner_line in f: > if re.search(pattern2, inner_line): > inner_line = f.next() Did you possibly elide a 'break' after the inner_line assignment? > Somewhat to my surprise, the code worked. Without a break, the inner loop will continue iterating through the rest of the file (billions of lines?) looking for pattern2 and re-binding inner-line if there is another line or raising StopIteration if there is not. Does this really constitute 'working'? This is quite aside from issue of what one wants if there is no pattern1 or if there is no line after the first match (probably not StopIteration) or if there is no pattern2. > I didn't know it was legal to do nested iterations over the same iterable Yes, but the effect is quite different for iterators (start where the outer iteration left off) and non-iterators (restart at the beginning). r = range(2) for i in r: for j in r: print(i,j) # this is a common idiom to get all pairs 0 0 0 1 1 0 1 1 ri= iter(range(3)) for i in ri: for j in ri: print(i,j) # this is somewhat deceptive as the outer loop executes just once 0 1 0 2 I personally would add a 'break' after 'outer_line = next(f)', since the first loop is effectively done anyway at that point, and dedent the second for statement. I find to following clearer ri= iter(range(3)) for i in ri: break for j in ri: print(i,j) # this makes it clear that the first loop executes just once 0 1 0 2 I would only nest if the inner loop could terminate without exhausting the iterator and I wanted the outer loop to then resume. __ Terry Jan Reedy