Sorted Returns List and Reversed Returns Iterator

John Machin sjmachin at lexicon.net
Fri Aug 22 05:40:42 CEST 2008


On Aug 22, 1:35 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Aug 22, 12:12 pm, "++imanshu" <himanshu.g... at gmail.com> wrote:
>
> > Hi,
>
> >      Is there a reason why two similarly named functions Sorted and
> > Reversed return different types of data or is it an accident.
>
> You seem to have an interesting notion of "similarly named".
> name0[-2:] == name1[-2:], perhaps? The two functions (eventually, in
> the case of "reversed") return data in the order one would expect from
> their names.
>
> >>> x = [1, 3, 5, 2, 4, 6]
> >>> sorted(x)
> [1, 2, 3, 4, 5, 6]
> >>> reversed(x)
>
> <listreverseiterator object at 0x00AA5550>
>
>
>
> >>> list(reversed(x))
> [6, 4, 2, 5, 3, 1]-

Sorry; having re-read the message subject:

reversed came later; returning an iterator rather than a list provides
more flexibility.

Cheers,
John



More information about the Python-list mailing list