Newbie question - sorting a slice

Discussion in 'Python' started by hwg, Aug 29, 2007.

  1. hwg

    hwg Guest

    I've searched the group and didn't see the answer to this...

    Why doesn't this work?:

    >>> letters = ['d', 'a', 'e', 'c', 'b']
    >>> letters[1:3].sort()
    >>>


    This returns None.

    Why? letters[1:3] is ['a', 'e', 'c'] Sorting that should return
    ['a', 'c', 'e']


    hwg
     
    hwg, Aug 29, 2007
    #1
    1. Advertising

  2. hwg

    Guest

    On Aug 28, 7:43 pm, hwg <> wrote:
    > I've searched the group and didn't see the answer to this...
    >
    > Why doesn't this work?:
    >
    > >>> letters = ['d', 'a', 'e', 'c', 'b']
    > >>> letters[1:3].sort()

    >
    > This returns None.
    >
    > Why? letters[1:3] is ['a', 'e', 'c'] Sorting that should return
    > ['a', 'c', 'e']
    >
    > hwg


    sort() works on a list in place, and returns none

    You have to make a temp list.

    note...
    >>> letters = ['d', 'a', 'e', 'c', 'b']
    >>> print letters.sort()

    None
    >>> print letters

    ['a', 'b', 'c', 'd', 'e']

    ~Sean
     
    , Aug 29, 2007
    #2
    1. Advertising

  3. hwg

    Paul McGuire Guest

    On Aug 28, 9:43 pm, hwg <> wrote:
    > I've searched the group and didn't see the answer to this...
    >
    > Why doesn't this work?:
    >
    > >>> letters = ['d', 'a', 'e', 'c', 'b']
    > >>> letters[1:3].sort()

    >
    > This returns None.
    >
    > Why? letters[1:3] is ['a', 'e', 'c'] Sorting that should return
    > ['a', 'c', 'e']
    >
    > hwg


    There are a couple of flaws in your example.

    letters[1:3] is not ['a', 'e', 'c'], but is just ['a','e']. The
    elements of a slice start with the lower-bound INclusive (zero-based,
    so this is 'a'), up to the upper bound EXclusive (up to, but NOT
    INCLUDING 'c'). The slice returns a copy of the selected elements.

    ..sort() does an in-place sort, and returns None. You may instead use
    the builtin function sorted(), as in:

    sorted(letters[1:4])

    which in fact DOES return ['a', 'c', 'e'].

    What your example had done was:
    1. choose 2 elements of a list and copy them to a temporary list
    2. sort the list in place
    3. return None
    4. discard the temporary list, since all references to it were gone

    For remedial study:
    - study notation for slices
    - study the difference between list.sort() and sorted()

    -- Paul
     
    Paul McGuire, Aug 29, 2007
    #3
    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. kr

    array slice question

    kr, Aug 10, 2004, in forum: Perl
    Replies:
    3
    Views:
    666
  2. 700MHz
    Replies:
    1
    Views:
    250
    Fredrik Lundh
    Oct 21, 2005
  3. George Sakkis

    Feature request: sorting a list slice

    George Sakkis, May 18, 2006, in forum: Python
    Replies:
    19
    Views:
    515
    Robert Kern
    May 21, 2006
  4. Giox
    Replies:
    5
    Views:
    731
    Martin Thompson
    Feb 27, 2007
  5. Rick
    Replies:
    9
    Views:
    123
    Ted Zlatanov
    Jul 31, 2007
Loading...

Share This Page