Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.040 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'python': 0.11; '2.7.3': 0.16; 'happily': 0.16; 'iterator,': 0.16; 'result:': 0.16; 'reversed': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.16; 'intermediate': 0.18; 'examples': 0.18; 'skip:" 30': 0.20; '31,': 0.22; '2015': 0.23; 'dec': 0.23; 'elements': 0.23; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.28; 'subject:use': 0.33; 'received:google.com': 0.34; 'to:addr:python- list': 0.35; 'list': 0.35; 'but': 0.36; 'list,': 0.36; 'subject:: ': 0.37; 'pm,': 0.39; 'method': 0.39; 'to:addr:python.org': 0.39; 'more': 0.62; 'construction': 0.73; 'denis': 0.84; 'to:name:python': 0.84; '2014,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=P0kvVSVRgi5cP9M/9ouV2uKaEIazH8R8O8on6/I88t4=; b=GpjCKwbZOh0jZ9JdTLFYlHIRj2r9ucnLKXyxqhhvo+riJALzW4dkkG9EpfVTCNwjo4 1TkzAwSf6kfcdmgzauwx9s7nRo7lnS6FYwgyj7INvL9cK9p1ULIVFZIgjVCbxh4X45hx M3nfHqx9d6HJdjw2GmDRNUlmjckzvQPxtAMOxqspng2JNRyMhnxOhwA9s6TC2HsrP+8Q HJk7/gBtiTfOV866myJN/AmhFxWd3u1b1Q7oE6qDcqR1S7vLpVENSY0JDgqxxTlGz22Q 7SY7cpbhy5Rv0ESMVtdi9iE1YwL4VH3maJSpf2FgkPwYv5S5Hj/xqJD29mKrXvlu+G/O NzgQ== X-Received: by 10.50.36.9 with SMTP id m9mr9849180igj.15.1433117213982; Sun, 31 May 2015 17:06:53 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <514c05fc-dded-4278-ac86-3e8e3cd0e851@googlegroups.com> From: Ian Kelly Date: Sun, 31 May 2015 18:06:13 -0600 Subject: Re: What use for reversed()? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433121593 news.xs4all.nl 2962 [2001:888:2000:d::a6]:46552 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91602 On Sun, May 31, 2015 at 1:58 PM, Denis McMahon wrote: > reversed returns an iterator, not a list, so it returns the reversed list > of elements one at a time. You can use list() or create a list from > reversed and then join the result: > > $ python > Python 2.7.3 (default, Dec 18 2014, 19:10:20) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> "".join(list(reversed("fred"))) > 'derf' >>>> "".join([x for x in reversed("fred")]) > 'derf' > > So reversed can do it, but needs a little help The str.join method will happily accept an iterator, so the intermediate list construction in those examples is unnecessary.