Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!novso.com!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.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'explicitly': 0.05; 'nested': 0.07; 'latter': 0.09; 'list).': 0.09; 'worked.': 0.09; '(x,': 0.16; '23,': 0.16; 'iterable': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'looping': 0.16; 'object)': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'object.': 0.31; 'file': 0.32; 'received:209.85': 0.35; 'case,': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'subject:?': 0.36; 'two': 0.37; 'received:209': 0.37; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'break': 0.61; 'today,': 0.61; 'simply': 0.61; 'different': 0.65; 'smith': 0.68; 'results': 0.69; 'legal': 0.71; 'guaranteed': 0.75; "else's": 0.84; 'replicate': 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:content-type; bh=sQKkPJ0SOoS32PRvCSwxsNmdo7iSmPFurRWsUvdfWGM=; b=c3kToLGKZTW68bZ8byJ8nWycSdBOhY0QnbFISZ42gZGH1zxgRKr4R/IKJoX3PFZnJp WM9+0vjzGgaAdzuZTPsuB4w8/anRGZH8O7gNYeHH2WVw7F2bNJmNieMYZk+v2NAatYWb 25e7d6u+3lFTDq9iTfzRb9QoF/2Sk4kVFwO3Je/41lCGSj/WQWLr79tAZxBuLK8YBRKn pSUQVhHAY8lx4oSFsqJNU2e+M/fjB5pJvQofaJERoDi9V7AxCZxPkttJTFlNE8R8MgIm HbV1iFDaiyYACHtyQO5AIzTmqSqxmOMl0h2hA93uCkpYmc0HGSlsWOd0LdxFFyA4vJjj w2mA== X-Received: by 10.68.176.197 with SMTP id ck5mr41331658pbc.165.1366733166425; Tue, 23 Apr 2013 09:06:06 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 23 Apr 2013 10:05:26 -0600 Subject: Re: Nested iteration? To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366733175 news.xs4all.nl 2244 [2001:888:2000:d::a6]:55630 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44194 On Tue, Apr 23, 2013 at 9: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() > > Somewhat to my surprise, the code worked. I didn't know it was legal > to do nested iterations over the same iterable (not to mention mixing > calls to next() with for-loops). Is this guaranteed to work in all > situations? Yes, although the results will be different depending on whether the iterable stores its iteration state on itself (like a file object) or in the iterator (like a list). In the latter case, you would simply have two independent simultaneous iterations of the same object. You can replicate the same effect in the latter case though by getting an iterator from the object and explicitly looping over the same iterator, like so: i = iter(range(10)) for x in i: if x % 4 == 1: for y in i: if y % 4 == 3: print("%d + %d = %d" % (x, y, x+y)) break