Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'newbie': 0.05; '[1,': 0.09; 'subject:skip:c 10': 0.09; 'subject:How': 0.10; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'am,': 0.29; 'subject:list': 0.30; "i'm": 0.30; 'figure': 0.32; 'convert': 0.35; 'subject:?': 0.36; 'list': 0.37; 'message- id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'helps': 0.61; 'subject:this': 0.83; '"j"': 0.84; 'square,': 0.84 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Mon, 16 Sep 2013 08:20:44 -0600 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How is this list comprehension evaluated? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379341258 news.xs4all.nl 15958 [2001:888:2000:d::a6]:44970 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54229 On 09/16/2013 07:43 AM, Arturo B wrote: > It uses a list comprenhension to generate the Latin Square, I'm am a newbie to Python, and I've tried to figure out how this is evaluated: > > a = [1, 2, 3, 4] > n = len(a) > [[a[i - j] for i in range(n)] for j in range(n)] > > I don't understand how the "i" and the "j" changes. > On my way of thought it is evaluated like this: It helps to convert it to a conventional for loop to see how it works: a = [1, 2, 3, 4] n = len(a) resultj = [] for j in range(n): resulti = [] for i in range(n): resulti.append(a[i-j]) resultj.append(resulti)