Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'args': 0.04; '5),': 0.09; '[1,': 0.09; 'chunks': 0.09; 'iterate': 0.09; 'python:': 0.09; 'slices': 0.09; 'def': 0.10; '(3,': 0.16; '(9,': 0.16; '*args)': 0.16; '2),': 0.16; 'apology': 0.16; 'iterable,': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'recipe': 0.16; 'recipes': 0.16; 'skip:z 30': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'module': 0.19; 'import': 0.21; 'needed.': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; '>>>>': 0.29; 'indentation': 0.29; 'print': 0.32; 'page.': 0.33; 'docs': 0.33; 'subject:List': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'done': 0.34; 'list': 0.35; 'skip:l 30': 0.35; 'so,': 0.35; 'there': 0.35; 'but': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'list,': 0.39; 'received:192.168': 0.40; 'group,': 0.60; 'email addr:gmail.com': 0.63; 'here': 0.65; 'learned': 0.65; 'dear': 0.66; 'kindly': 0.67; 'subject:For': 0.75; 'abc': 0.91 Date: Sun, 13 Jan 2013 12:41:39 -0500 From: Mitya Sirenef 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; format=flowed Content-Transfer-Encoding: 7bit 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358098902 news.xs4all.nl 6865 [2001:888:2000:d::a6]:52619 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36743 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] > 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? > > If anyone of the learned members may kindly let me know? > > Apology for any indentation error,etc. > > Thanking You in Advance, > > Regards, > Subhabrata > > > > Another good answer is to use a recipe from itertools docs page. There are a lot of good recipes there and you may want to keep them all in a module you can import from when needed. Here is the recipe: def grouper(n, iterable, fillvalue=None): """From itertools recipes: collect data into fixed-length chunks or blocks.""" # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return zip_longest(fillvalue=fillvalue, *args) >>> list(grouper(3, range(12)) ... ... ... ... ) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)] HTH, - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/