Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'output': 0.05; '"""': 0.07; 'method.': 0.07; 'arguments': 0.09; 'strings.': 0.09; 'cc:addr:python-list': 0.11; 'assume': 0.14; '2.6:': 0.16; 'iterators': 0.16; 'loops': 0.16; 'nest': 0.16; 'possible?': 0.16; 'reversed': 0.16; 'reverses': 0.16; 'subject:make': 0.16; 'subject:run': 0.16; 'xavier': 0.16; 'wrote:': 0.18; '(the': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'integer': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'possibility': 0.29; 'message- id:@mail.gmail.com': 0.30; 'run': 0.32; 'skip:_ 10': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'sequence': 0.36; 'method': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'list': 0.37; 'received:209': 0.37; 'easily': 0.37; 'starting': 0.37; 'anything': 0.39; 'does': 0.39; 'changed': 0.39; 'most': 0.60; 'new': 0.61; 'march': 0.61; 'show': 0.63; 'reverse': 0.68; 'advertises': 0.84; 'oscar': 0.84; 'subject:you': 0.87; 'to:addr:contact': 0.91; '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:cc:content-type; bh=4hd4v8CSDqLMu0cYdIOpIVlrUfKs9aGeGSQy20vFiWA=; b=qP/AhdGUe82GL5o8IBJXyoewBSrBfZiD9awJMftZ2o+uCfUQ6B1MxIusKcxorwss8x Uy3fRI+TEb3iE6EnP4kFrj32PqP/jW00yt0vlt+qvptWmNV8eZWo8T+16L4fACjF8w/U Jq+DWsUae9UAerOK7uPhirVy1i0ZjXbiOtDLEUTaSBTARglEvg/e7RqSmQ8isaKOA4p3 T6/0Oak1k894YCeQSoDbtjlaHUhQ9h3C1i+4OPaGTXVWF9c6X4DHgeRUviPTtAhn+kjh 7nJ2N6KcDyB7dVsqmCTl2rRkC0fNGPzvRXXJJII9iiKo+tfNPZFByeKx4BnAp0Z+ZlII r2TQ== X-Received: by 10.220.140.18 with SMTP id g18mr21754757vcu.54.1364346545275; Tue, 26 Mar 2013 18:09:05 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Wed, 27 Mar 2013 01:08:45 +0000 Subject: Re: how do you make a loop run in reverse? To: contact@xavierho.com Content-Type: text/plain; charset=ISO-8859-1 Cc: "python-list@python.org" , rahulreddy24@hotmail.com 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364346554 news.xs4all.nl 6986 [2001:888:2000:d::a6]:42015 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41971 > On 27 March 2013 10:59, wrote: >> >> So i have a set of for loops that create this : >> [snip] >> but i want to nest all the loops under one BIG loop that'll run in reverse >> to make this: [snip] >> Is this possible? On 27 March 2013 00:19, Xavier Ho wrote: > There is a built-in function that reverses an iterable. Have a look at the > documentation. I assume you mean the reversed function. It does not in general reverse an iterable. From the docs: """ reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0). New in version 2.4. Changed in version 2.6: Added the possibility to write a custom __reversed__() method. """ So it reverses a sequence (having the __len__ and __getitem__ methods) or an object that advertises reversibility (having a __reversed__ method). It does not reverse anything else including generators, iterators and most non-sequence iterables. To the OP: To reverse a "set of for loops" as requested is not possible using the reversed function. It is, however, possible to reverse a list of strings. So if you have a function that returns the list of strings you show as output then you can easily reverse that list with reversed(mylist) or mylist[::-1]. Oscar