Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91602

Re: What use for reversed()?

References <514c05fc-dded-4278-ac86-3e8e3cd0e851@googlegroups.com> <mkfp4q$lkf$3@dont-email.me>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-05-31 18:06 -0600
Subject Re: What use for reversed()?
Newsgroups comp.lang.python
Message-ID <mailman.254.1433121593.5151.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, May 31, 2015 at 1:58 PM, Denis McMahon <denismfmcmahon@gmail.com> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 12:40 -0700
  Re: What use for reversed()? Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-31 19:58 +0000
    Re: What use for reversed()? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-31 18:06 -0600
  Re: What use for reversed()? cheshire <supportNOSPAM@microsoft.com> - 2015-05-31 23:26 +0200
  Re: What use for reversed()? Tim Delaney <timothy.c.delaney@gmail.com> - 2015-06-01 09:23 +1000
    Re: What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 20:11 -0700
  Re: What use for reversed()? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-01 01:30 +0100
  Re: What use for reversed()? Tim Delaney <timothy.c.delaney@gmail.com> - 2015-06-01 13:59 +1000
  Re: What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 22:15 -0700
    Re: What use for reversed()? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-01 09:11 +0300

csiph-web