Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'syntax': 0.03; '[1,': 0.09; 'slices': 0.09; 'cc:addr:python-list': 0.10; '-tkc': 0.16; '12]': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'math': 0.20; 'error.': 0.21; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '>>>>': 0.29; 'print': 0.32; '11,': 0.33; 'subject:List': 0.33; 'or,': 0.34; 'done': 0.34; 'list': 0.35; 'skip:l 30': 0.35; 'so,': 0.35; 'item': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'skip:l 20': 0.38; 'little': 0.39; 'group,': 0.60; 'email addr:gmail.com': 0.63; 'dear': 0.66; 'subject:For': 0.75; 'received:50.22': 0.84 Date: Sun, 13 Jan 2013 07:29:20 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: subhabangalore@gmail.com Subject: Re: For Loop in List References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358083679 news.xs4all.nl 6951 [2001:888:2000:d::a6]:58766 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36733 On 01/13/13 06:45, subhabangalore@gmail.com wrote: > Dear Group, > > I have a list like, > >>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12] > > Now, if I want to take a slice of it, I can. > It may be done in, >>>> list2=list1[:3] >>>> print list2 [snip] > 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] Well, you get a SyntaxError because, well, it's a syntax error. It may take a little math to do this: >>> SIZE = 3 >>> for i in range(len(list1)//SICE): ... print list1[i*SIZE:i*SIZE+SIZE] ... [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] Or, you can exploit the fact that iterators exhaust inside a for-loop: >>> i = iter(list1) >>> for item in i: ... print [item, i.next(), i.next()] ... [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] Hope this helps, -tkc