Sorted Returns List and Reversed Returns Iterator

Discussion in 'Python' started by ++imanshu, Aug 22, 2008.

  1. ++imanshu

    ++imanshu Guest

    Hi,

    Is there a reason why two similarly named functions Sorted and
    Reversed return different types of data or is it an accident.

    Thanks,
    ++imanshu
     
    ++imanshu, Aug 22, 2008
    #1
    1. Advertising

  2. ++imanshu

    John Machin Guest

    On Aug 22, 12:12 pm, "++imanshu" <> 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]
    >>>
     
    John Machin, Aug 22, 2008
    #2
    1. Advertising

  3. ++imanshu

    John Machin Guest

    On Aug 22, 1:35 pm, John Machin <> wrote:
    > On Aug 22, 12:12 pm, "++imanshu" <> 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
     
    John Machin, Aug 22, 2008
    #3
  4. ++imanshu

    ++imanshu Guest

    On Aug 22, 8:40 am, John Machin <> wrote:
    > On Aug 22, 1:35 pm, John Machin <> wrote:
    >
    >
    >
    > > On Aug 22, 12:12 pm, "++imanshu" <> 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


    I agree. Iterator is more flexible. Together and both might have
    returned the same types.

    Thanks,
    ++imanshu
     
    ++imanshu, Aug 22, 2008
    #4
  5. ++imanshu

    Peter Otten Guest

    ++imanshu wrote:

    > On Aug 22, 8:40 am, John Machin <> wrote:
    >> On Aug 22, 1:35 pm, John Machin <> wrote:
    >>
    >>
    >>
    >> > On Aug 22, 12:12 pm, "++imanshu" <> 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

    >
    > I agree. Iterator is more flexible. Together and both might have
    > returned the same types.


    It's easy to generate a reversed sequence on the fly but impractical for a
    sorted one. Python is taking the pragmatic approach here.

    Peter
     
    Peter Otten, Aug 22, 2008
    #5
  6. John Machin wrote:

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


    As in flexibility for the implementer, the day someone invents a sort
    algorithm that doesn't have to look at all source items before it starts
    producing output?

    Because I fail to see how returning an object that supports forward
    iteration only is more flexible for the *user* than returning an object
    that supports random access, mutation, restartable iteration, *and*
    forward iteration.

    </F>
     
    Fredrik Lundh, Aug 22, 2008
    #6
  7. ++imanshu

    Terry Reedy Guest

    Peter Otten wrote:
    > ++imanshu wrote:


    >> I agree. Iterator is more flexible.


    I disagree. Neither is more flexible. You can iter the list returned
    by sorted and list the iter returned by reversed. Both do the minimum
    work necessary. See below.

    > Together and both might have returned the same types.


    True, but only by doing potentially unnecessary work and requiring the
    caller to do potentially unnecessary work that might even prevent the
    program from working. This is less flexible.

    Suppose sorted now returns alist with 50 million items. Suppose it
    instead returned iter(alist) but the caller wants to randomly index the
    items. Since the caller could not access the existing 50 million item
    list, the caller would have to make another 50 million item copy. This
    is non-trivial and might not even work do to memory limitations.

    > It's easy to generate a reversed sequence on the fly but impractical for a
    > sorted one. Python is taking the pragmatic approach here.


    To expand on this: sorting and reversing are algorithmically different
    operations. Sorting requires that one have all items in hand in a
    mutable sequence (list) for arbitrary re-ordering. Sorted works on any
    iterable and starts by making a new list. There is no point to not
    returning that list after it is sorted. It would be more work and less
    useful to do more.

    sorted(iterable, key=None, reverse=False):
    newlist = list(iterable)
    newlist.sort(key, reverse)
    return newlist

    Iterating over a concrete sequence in reverse order, on the other hand,
    is trivial. It would be more work and less useful to do more.

    def _reversed(seq): # 'hidden' generator function
    n = len(seq)
    while n:
    n -= 1
    yield seq[n]

    def reversed(seq):
    if hasattr(seq, '__reversed__'):
    return seq.__reversed__() # I presume this is tried first
    else:
    return _reversed(seq) # generic fall-back

    Terry Jan Reedy
     
    Terry Reedy, Aug 22, 2008
    #7
  8. ++imanshu

    ++imanshu Guest

    On Aug 22, 12:36 pm, Terry Reedy <> wrote:
    > Peter Otten wrote:
    > > ++imanshu wrote:
    > >> I agree. Iterator is more flexible.

    >
    > I disagree.  Neither is more flexible.  You can iter the list returned
    > by sorted and list the iter returned by reversed.  Both do the minimum
    > work necessary.  See below.
    >
    >  > Together and both might have returned the same types.
    >
    > True, but only by doing potentially unnecessary work and requiring the
    > caller to do potentially unnecessary work that might even prevent the
    > program from working.  This is less flexible.
    >
    > Suppose sorted now returns alist with 50 million items.  Suppose it
    > instead returned iter(alist) but the caller wants to randomly index the
    > items.  Since the caller could not access the existing 50 million item
    > list, the caller would have to make another 50 million item copy.  This
    > is non-trivial and might not even work do to memory limitations.
    >
    > > It's easy to generate a reversed sequence on the fly but impractical for a
    > > sorted one. Python is taking the pragmatic approach here.

    >
    > To expand on this: sorting and reversing are algorithmically different
    > operations.   Sorting requires that one have all items in hand in a
    > mutable sequence (list) for arbitrary re-ordering.  Sorted works on any
    > iterable and starts by making a new list.  There is no point to not
    > returning that list after it is sorted.  It would be more work and less
    > useful to do more.
    >
    > sorted(iterable, key=None, reverse=False):
    >    newlist = list(iterable)
    >    newlist.sort(key, reverse)
    >    return newlist
    >
    > Iterating over a concrete sequence in reverse order, on the other hand,
    > is trivial.  It would be more work and less useful to do more.
    >
    > def _reversed(seq): # 'hidden' generator function
    >    n = len(seq)
    >    while n:
    >      n -= 1
    >      yield seq[n]
    >
    > def reversed(seq):
    >    if hasattr(seq, '__reversed__'):
    >      return seq.__reversed__() # I presume this is tried first
    >    else:
    >      return _reversed(seq) # generic fall-back
    >
    > Terry Jan Reedy


    Thanks for giving the 'behind the scenes' reasons. It looks
    reasonable now.

    Thank You,
    ++imanshu
     
    ++imanshu, Aug 23, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gyruss
    Replies:
    1
    Views:
    3,993
    John C. Bollinger
    Apr 11, 2005
  2. David Li

    the byte be reversed! why?

    David Li, Apr 24, 2005, in forum: C++
    Replies:
    7
    Views:
    460
    David Li
    Apr 24, 2005
  3. mark
    Replies:
    2
    Views:
    284
    Dave K
    Jan 3, 2004
  4. Replies:
    13
    Views:
    435
    Sion Arrowsmith
    Nov 6, 2006
  5. Replies:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
Loading...

Share This Page