Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'syntax': 0.03; 'compiler': 0.05; 'interpreted': 0.07; '[1,': 0.09; 'iterate': 0.09; 'loop.': 0.09; 'slices': 0.09; 'thrown': 0.09; 'toss': 0.09; 'apology': 0.16; 'colon.': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'shallow': 0.16; 'temp': 0.16; 'wrote:': 0.17; 'equivalent': 0.20; 'python?': 0.20; 'error.': 0.21; 'modifying': 0.22; "i'd": 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; "doesn't": 0.28; '>>>>': 0.29; 'indentation': 0.29; 'included': 0.29; "we're": 0.30; 'code': 0.31; 'could': 0.32; 'print': 0.32; 'subject:List': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'done': 0.34; 'list': 0.35; 'false': 0.35; 'skip:l 30': 0.35; 'so,': 0.35; 'doing': 0.35; 'similar': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'subject:: ': 0.38; 'gives': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'list,': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'group,': 0.60; 'first': 0.61; 'email addr:gmail.com': 0.63; 'learned': 0.65; 'dear': 0.66; 'kindly': 0.67; 'header:Reply- To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'subject:For': 0.75; "'for'": 0.84; "it'd": 0.84; 'received:74.208.4.194': 0.84; 'examine': 0.95 Date: Sun, 13 Jan 2013 08:20:57 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: For Loop in List References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:EVqtiHzh0Iei7TdCbjmSDSa35z2tkL2OVY0YRYThjoN G3vncClsA7AaLoxNPIaJ8Rt+mmnweCrG4hBh2A9YuKYecubfwQ WQNFZiddBhIScUEcYvyST9K90RkE7hvfDWpaGf5vvb4lydLm7H E68BTQEbiaM2MvSd95pf0JO//wtjB6XeDRFzwtLxoH5DAGx4z7 fyW4Hzfyix7Jjv/g/lnibjuLz2hEiG+mK3IiKRSZTz3WLl+h+i nLg48vB5RMgUM0DEM5fdj/6zviei8LrN2yVmjgZFAdS2BTOsVG OANdH0frTIxWchLIr8sjPIkyJBO7qd0LApLNAC/782/35Tknw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358083283 news.xs4all.nl 6861 [2001:888:2000:d::a6]:54229 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36732 On 01/13/2013 07:45 AM, subhabangalore@gmail.com wrote: > Dear Group, > > I have a list like, > >>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12] What version of Python? > Now, if I want to take a slice of it, I can. > It may be done in, >>>> list2=list1[:3] >>>> print list2 > [1, 2, 3] > > If I want to iterate the list, I may do as, > >>>> for i in list1: > print "Iterated Value Is:",i > > > Iterated Value Is: 1 > Iterated Value Is: 2 > Iterated Value Is: 3 > Iterated Value Is: 4 > Iterated Value Is: 5 > Iterated Value Is: 6 > Iterated Value Is: 7 > Iterated Value Is: 8 > Iterated Value Is: 9 > Iterated Value Is: 10 > Iterated Value Is: 11 > Iterated Value Is: 12 > > Now, I want to combine iterator with a slicing condition like > >>>> for i=list2 in list1: > print "Iterated Value Is:",i > > So, that I get the list in the slices like, > [1,2,3] > [4,5,6] > [7,8,9] > [10,11,12] > > But if I do this I get a Syntax Error, is there a solution? It'd be only polite if you actually included the traceback, instead of paraphrasing the error. > If anyone of the learned members may kindly let me know? > > Apology for any indentation error,etc. > > Thanking You in Advance, > > Regards, > Subhabrata > > > > let's examine the code that generates the syntax error. for i=list2 in list1: That doesn't match any of the grammar of Python, so it gives a syntax error. How could the compiler have interpreted it? Perhaps it could have thrown out the 'for' and the colon. That would be equivalent in this case to: i = False or we could toss out the "=list2" but that would give us your first loop. If I were doing this, I'd do something like (untested): temp = list1[:] #make a shallow copy of the list, so we're not modifying the original while temp print temp[:3] temp = temp[3:] I think you could do something similar with zip, but I don't have the energy this morning. -- DaveA